C# 仅在调试版本中调用Bitmap.Save(…;)时发生外部异常
我使用下面的代码输出我的计算结果,它应该是一个PNG图片。我不明白为什么在调试运行中,最后一行给了我一个C# 仅在调试版本中调用Bitmap.Save(…;)时发生外部异常,c#,.net,system.drawing,system.drawing.imaging,C#,.net,System.drawing,System.drawing.imaging,我使用下面的代码输出我的计算结果,它应该是一个PNG图片。我不明白为什么在调试运行中,最后一行给了我一个System.Runtime.InteropServices.ExternalException。在发布运行中,一切正常 我在网上发现,当图像被代码的其他部分使用时,可能会发生此错误,但在我的情况下,这不是真的 //using System; //using System.Drawing; //using System.Drawing.Imaging; Bitmap png = new Bi
System.Runtime.InteropServices.ExternalException
。在发布运行中,一切正常
我在网上发现,当图像被代码的其他部分使用时,可能会发生此错误,但在我的情况下,这不是真的
//using System;
//using System.Drawing;
//using System.Drawing.Imaging;
Bitmap png = new Bitmap(this.xPixels, this.yPixels, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(png);
g.Clear(Color.White);
g.DrawString(currentTime, myFont, mySolidBrush, timeX, timeY, myXTitleFormat);
// write image to file
string path4 = string.Concat(Environment.CurrentDirectory, @"\Output\T1\" + this.fileName + ".png");
png.Save(path4, ImageFormat.Png);
上面说“图像是用错误的图像格式保存的”
同样奇怪的是,您使用图形
对象进行操作,但使用位图
进行保存。使用g
进行的操作是否会影响位图
以下代码在调试或发布模式下引发异常:
{
Bitmap png = new Bitmap(100, 100, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(png);
g.Clear(Color.White);
g.DrawString("dummy", SystemFonts.DefaultFont, Brushes.Beige, RectangleF.FromLTRB(5,5,80,80), StringFormat.GenericDefault);
// write image to file
string path4 = @"C:\test.png";
png.Save(path4, System.Drawing.Imaging.ImageFormat.Png);
}
您是否可以检查您是否有权限将文件写入指定的文件夹?您是否尝试在调试/发布中更改路径,例如“C:\test.png”?@Onur:实际上,输出文件夹在我的发布文件夹中。它会导致此错误吗?我会尽可能消除调试/发布之间的差异,并尝试尽可能多地硬编码(如路径)。永远不要在代码中使用Environment.CurrentDirectory。它不是您希望的目录的可能性太大了。没有“\Output\T1\”子目录时打开。乘以没有写访问权限的几率,就像任何c:\program files子目录一样。改用AppData。