Общая ошибка в GDI + возникает в Снимке экрана
Я пишу приложение, которое делает снимки экрана, но случайно оно выдаст общую ошибку GDI +. К сожалению, общая ошибка не помогает мне отладить хорошо. Мой код для захвата экрана:
static void CaptureScreen()
{
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(savePath + "img" + times.ToString()+ ".png", ImageFormat.Png);
}
Что иногда дает мне общую ошибку, поэтому я подумал:Может быть, я должен избавиться от растровых и графических переменных "
static void CaptureScreen()
{
bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
// Create a graphics object from the bitmap
gfxScreenshot = Graphics.FromImage(bmpScreenshot);
// Take the screenshot from the upper left corner to the right bottom corner
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
// Save the screenshot to the specified path that the user has chosen
bmpScreenshot.Save(savePath + "img" + times.ToString()+ ".png", ImageFormat.Png);
bmpScreenshot.Dispose();
gfxScreenshot.Dispose();
}
а затем удалите указанный файл:
for (int i = 1; i == times; i++)
{
File.Delete(savePath + @"\img" + i.ToString() + ".png");
}
times = 0;
Но если вы запускаете это дважды, это говорит о том, что файл используется, если вы пишете в тот же файл. Есть идеи?