[英]A generic error occurred in GDI+ exception when saving bitmap
I'm trying to save a file in my project bin folder. 我正在尝试将文件保存在我的项目bin文件夹中。 For some reason when I give it a string as a path such as 由于某种原因,当我给它一个字符串作为路径时,例如
string filePath = @"C:\Users\Craig\Documents\Visual Studio 2015\Projects\CreateTextExample\CreateTextExample\bin\ErrorLog";
Thread.Sleep(100);
bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);
it saves the file fine. 它可以很好地保存文件。 However when I try and save it like 但是当我尝试保存它时
string filePath = System.IO.Directory.GetCurrentDirectory() + @"\ErrorLog";
Thread.Sleep(100);
bitmap.Save(filePath+@"\ErrorImage.Bmp", ImageFormat.Bmp);
I get a runtime error saying A generic error occurred in GDI+
我收到一个运行时错误消息说A generic error occurred in GDI+
Not sure why this is happening. 不知道为什么会这样。 Initially I thought it may have been the folder permissions however this doesn't seem to be the case as it works using the first method. 最初我以为可能是文件夹权限,但是事实并非如此,因为使用第一种方法就可以了。
Any ideas why this is happening? 任何想法为什么会这样?
My code is as follows 我的代码如下
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);
When using the Save method of the Bitmap
you should ensure that the directory exist. 使用Bitmap
的Save方法时,应确保该目录存在。 In the first case your directory is this: 在第一种情况下,您的目录是这样的:
This directory should exist in your system 该目录应该存在于您的系统中
C:\\Users\\Craig\\Documents\\Visual Studio 2015\\Projects\\CreateTextExample\\CreateTextExample\\bin\\ErrorLog
But in the second case (when using Directory.GetCurrentDirectory
method) your directory should be something like this (it may have a extera Debug
or Release
folder before the ErrorLog
) 但是在第二种情况下(使用Directory.GetCurrentDirectory
方法时),您的目录应该是这样的(在ErrorLog
之前,它可能具有extera Debug
或Release
文件夹)
These directories should not exist in your system (depending you are in Debug or Release Mode) 这些目录在您的系统中不应该存在(取决于您是处于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
So bitmap.Save
throws error because the directory does not exists in your system. 因此,由于该目录在您的系统中不存在,因此bitmap.Save
会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.