保存位图时GDI +异常中发生一般错误

时间:2017-05-16 15:38:40

标签: c# .net bitmap gdi+

我试图将文件保存在我的项目bin文件夹中。出于某种原因,当我给它一个字符串作为路径,如

GROUP_CONCAT(DISTINCT patient_id) AS patient_ids

它可以保存文件。但是,当我尝试将其保存为

string filePath = @"C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\ErrorLog";
Thread.Sleep(100);
bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);

我收到运行时错误string filePath = System.IO.Directory.GetCurrentDirectory() + @"\ErrorLog"; Thread.Sleep(100); bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);

不确定为什么会这样。最初我认为它可能是文件夹权限,但是这似乎并非如此,因为它使用第一种方法。enter image description here

为什么会发生这种情况?

我的代码如下

A generic error occurred in GDI+

1 个答案:

答案 0 :(得分:3)

使用Bitmap的Save方法时,应确保该目录存在。在第一种情况下,您的目录是:

此目录应存在于您的系统中

C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\ErrorLog

但在第二种情况下(使用Directory.GetCurrentDirectory方法时),您的目录应该是这样的(它可能在Debug之前有一个外部ReleaseErrorLog文件夹)

您的系统中不应存在这些目录(取决于您处于调试或发布模式)

C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\Debug\ErrorLog

C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\Release\ErrorLog

因此bitmap.Save会引发错误,因为您的系统中不存在该目录。