Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

c# - A Generic error occurs at GDI+ at Bitmap.Save() after using SaveFileDialog

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);                            
    // ...
}

But I still get the error:

A Generic Error occured at GDI+

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.

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?

Edit: System.Drawing.Image to System.Drawing.Bitmap as the type of tempImg.

Edit: I have identified I get this error only after SaveFileDialog is created and user clicks on 'Save'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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....

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 :

Path specified by SaveFileDialog + the relative path

automatically.

Thus if the path does not exist, this error occurs.

Thanks every one for the answers.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...