Generic error in GDI+ occurs in Screen Capture

时光总嘲笑我的痴心妄想 提交于 2019-12-31 06:54:08

问题


I am writing an application that takes screen shots, but randomly it will throw a GDI+ generic error. Unfortunately, a generic error does not help me debug well. My code for the screen capture is:

    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);
    }

Which will give me a generic error occasionally, so I thought "Maybe I should dispose the bitmap and graphics variable"

    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();
    }

and then delete said file:

            for (int i = 1; i == times; i++)
            {
                File.Delete(savePath + @"\img" + i.ToString() + ".png");
            }
            times = 0;

But if you run that twice it says the file is in use, if you are writing to the same file. Any ideas?


回答1:


The following might help:

using (bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb))
using (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);

    Bitmap bmpScreenshot2 = new Bitmap(bmpScreenshot); // FIX: Change "S" to "s"...
    // Save the screenshot to the specified path that the user has chosen
    bmpScreenshot2.Save(savePath + "img" + times.ToString()+ ".png", ImageFormat.Png);
}


来源:https://stackoverflow.com/questions/15971251/generic-error-in-gdi-occurs-in-screen-capture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!