C#:调试GDI +接口中的泛型错误?

问题描述 投票:0回答:1

如何轻松调试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

c# .net .net-4.5 gdi+
1个回答
2
投票

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);
© www.soinside.com 2019 - 2024. All rights reserved.