保存位图时出现 GDI+ 异常的一般错误
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
方法时)你的目录应该是这样的(它可能有一个 extera Debug
或 Release
文件夹在 ErrorLog
)
您的系统中不应存在这些目录(取决于您处于调试模式还是发布模式)
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
抛出错误,因为该目录在您的系统中不存在。
我正在尝试将文件保存到我的项目 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
方法时)你的目录应该是这样的(它可能有一个 extera Debug
或 Release
文件夹在 ErrorLog
)
您的系统中不应存在这些目录(取决于您处于调试模式还是发布模式)
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
抛出错误,因为该目录在您的系统中不存在。