GDI+ 中的一般错误发生在 Screen Capture 中 [英] Generic error in GDI+ occurs in Screen Capture
本文介绍了GDI+ 中的一般错误发生在 Screen Capture 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个用于截屏的应用程序,但它会随机抛出 GDI+ 一般错误.不幸的是,一般错误并不能帮助我很好地调试.我的屏幕截图代码是:
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();
}
然后删除该文件:
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?
推荐答案
以下内容可能会有所帮助:
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);
}
这篇关于GDI+ 中的一般错误发生在 Screen Capture 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文