問題描述
我使用以下代碼塊,并在 using 塊中添加了更多代碼:
I use the following code block with some more code inside the using block:
using (System.Drawing.Bitmap tempImg =
(System.Drawing.Bitmap)tempObj.GetData(System.Windows.Forms.DataFormats.Bitmap))
{
// ...
tempImg.Save("..\..\testdata\tempImg.bmp", ImageFormat.Bmp);
// ...
}
但我仍然收到錯誤:
GDI+ 出現一般錯誤
A Generic Error occured at GDI+
只有在我做了一些與 using 塊內的代碼無關的操作之后.在其他時候,這很有效.tempImg.bmp 也是一個臨時文件,所以我刪除了 using 塊本身中的 tempImg.bmp.
only after I make some action which is not related to the code inside the using block. In other times this works well. Also the tempImg.bmp is a temporary file, so I delete the tempImg.bmp within the using block itself.
由于 tempImg 在 using 里面并且它被處理了,我認為應該解決鎖定問題.
Since the tempImg is inside the using and this it's disposed, I think the locking problem should be solved.
有人可以讓我知道這段代碼中的錯誤是什么嗎?
Can someone please let me know what is the mistake in this code?
System.Drawing.Image
到 System.Drawing.Bitmap
作為 tempImg
的類型.
System.Drawing.Image
to System.Drawing.Bitmap
as the type of tempImg
.
我已經確定只有在創建 SaveFileDialog 并且用戶單擊保存"后才會收到此錯誤.
I have identified I get this error only after SaveFileDialog is created and user clicks on 'Save'.
推薦答案
我終于可以找到我的代碼有什么問題了,我想在這里提一下,因為我認為它可能對某人有用....
Finally I could find what was wrong in my code and would like to mention it here as I think it may be useful to someone....
因為我在 tempImg.Save 中給出了一個相對路徑,在用戶點擊 SaveFileDialog 中的Save"后,tempImg.Save 的實際路徑變為:
As I have given a relative path in tempImg.Save, and after the user clicks 'Save' in SaveFileDialog, the actual path for tempImg.Save become :
SaveFileDialog指定的路徑+相對路徑
Path specified by SaveFileDialog + the relative path
自動.
因此,如果路徑不存在,則會發生此錯誤.
Thus if the path does not exist, this error occurs.
謝謝大家的回答.
這篇關于使用 SaveFileDialog 后,在 Bitmap.Save() 的 GDI+ 處發生一般錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!