簡體   English   中英

保存位圖時,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方法時),您的目錄應該是這樣的(在ErrorLog之前,它可能具有extera DebugRelease文件夾)

這些目錄在您的系統中不應該存在(取決於您是處於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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM