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

Dulini Atapattu picture Dulini Atapattu · Apr 28, 2011 · Viewed 48k times · Source

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

Answer

Dulini Atapattu picture Dulini Atapattu · May 5, 2011

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.