Ein allgemeiner Fehler in GDI + tritt in Screen Capture auf
Ich schreibe eine Anwendung, die Screenshots macht, aber zufällig einen generischen GDI + -Fehler auslöst. Leider hilft mir ein generischer Fehler nicht beim Debuggen. Mein Code für die Bildschirmaufnahme lautet:
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);
}
Wodurch ich gelegentlich einen generischen Fehler erhalte, dachte ich mir: "Vielleicht sollte ich die Bitmap- und Grafikvariable entsorgen."
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();
}
und dann lösche die Datei:
for (int i = 1; i == times; i++)
{
File.Delete(savePath + @"\img" + i.ToString() + ".png");
}
times = 0;
Wenn Sie dies jedoch zweimal ausführen, wird die Datei verwendet, wenn Sie in dieselbe Datei schreiben. Irgendwelche Ideen?