[英]A Generic Error occurred in GDI+ when saving bitmap to MemoryStream
[英]A generic error occurred in GDI+ exception when saving bitmap
我正在尝试将文件保存在我的项目bin文件夹中。 由于某种原因,当我给它一个字符串作为路径时,例如
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);
我收到一个运行时错误消息说A generic error occurred in GDI+
不知道为什么会这样。 最初我以为可能是文件夹权限,但是事实并非如此,因为使用第一种方法就可以了。
任何想法为什么会这样?
我的代码如下
string currentContent = String.Empty;
bitmap = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as Image);
graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\ErrorLog";
Thread.Sleep(100);
bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);
使用Bitmap
的Save方法时,应确保该目录存在。 在第一种情况下,您的目录是这样的:
该目录应该存在于您的系统中
C:\\Users\\Craig\\Documents\\Visual Studio 2015\\Projects\\CreateTextExample\\CreateTextExample\\bin\\ErrorLog
但是在第二种情况下(使用Directory.GetCurrentDirectory
方法时),您的目录应该是这样的(在ErrorLog
之前,它可能具有extera Debug
或Release
文件夹)
这些目录在您的系统中不应该存在(取决于您是处于Debug还是Release模式)
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
会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.