如何轻松调试GDI +接口中发生的一般错误?
我正在尝试保存位图文件。它发生在Bitmap.Save(...)调用上。
码:
var imagePath = @"C:\test.bmp";
//File.Create(imagePath);
Bitmap b = new Bitmap(100, 100);
for (int pixelXCounter = 0; pixelXCounter < 100; pixelXCounter++)
{
for (int pixelYCounter = 0; pixelYCounter < 100; pixelYCounter++)
{
b.SetPixel(pixelXCounter, pixelYCounter, System.Drawing.Color.Salmon);
}
}
b.Save(imagePath, System.Drawing.Imaging.ImageFormat.Bmp);
更新:
所以这里有一些关于我收到的错误的更多信息(在我注释掉File.Creat()之后):
发生ExternalException:“System.Drawing.dll中发生'System.Runtime.InteropServices.ExternalException'类型的第一次机会异常
附加信息:GDI +中发生一般错误。 “
异常对象的ErrorCode:-2147467259
File.Create()的剂量是多少?它锁定文件,您的位图无法保存在磁盘上。去掉它 :
var imagePath = @"C:\test.bmp";
Bitmap b = new Bitmap(100, 100);
for (int pixelXCounter = 0; pixelXCounter < 100; pixelXCounter++) {
for (int pixelYCounter = 0; pixelYCounter < 100; pixelYCounter++)
{
b.SetPixel(pixelXCounter, pixelYCounter, System.Drawing.Color.Salmon);
} }
b.Save(imagePath, System.Drawing.Imaging.ImageFormat.Bmp);