"A generic error occurred in GDI+" when attempting to use Image.Save

37,629

Solution 1

Thank you to Simon Whitehead for answering this in the comments. He said, "3) Make sure the file is not in use by anything else (including your code)."

So the problem was that my own code was using the item.Image object, and was preventing GDI+ to call the dispose() method on it. The solution was to copy the object into a new object, then use that object to "Write." The resulting code is as follows:

try
{
   using (Bitmap tempImage = new Bitmap(item.Image)) 
   {
      tempImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
   }    
}
catch (Exception e)
{
    Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
}

Solution 2

I too faced same error for code line:

wmImg.Save(BrandImgPath,ImageFormat.Png);

BrandImgPath = "D:/XYZ/fileName;

Found cause:

XYZ folder didn't exist in D: drive. So my code was creating this folder later. One should ensure if that path exist or not.

if (Directory.Exists(@"D:/XYZ")) return;

Hope it will help someone to solve his code mistakes.

Solution 3


1. Make Sure That your destination folder have read/write permission (check it twice!).
2. Using Server.MapPath is better
3. Make Sure you have free space on your destination drive or folder. 4. Most of the times we cant user Memory Streamers On Shared Servers, So we should be make sure that provider allow us to use it.

Hope Microsoft Give Detailed Exception Errors instead of "Generic GDI+ Errror" !!!

Solution 4

Had this issue myself, needed to check that the folder existed, GDI didn't tell me what went wrong, would have been nice.

Share:
37,629
Magnum
Author by

Magnum

Updated on October 01, 2020

Comments

  • Magnum
    Magnum over 3 years

    I am developing an Outlook 2010 Add-In, and am loading an image from a serialized XML file. The image loads fine, and am able to assign it to a pictureBox object on a Winform no problem. The object is saved in

    [XmlIgnore]
    public Bitmap Image
    {
       get { return this.templateImage; }
       set { this.templateImage = value; }
     }
    

    When, I attempt to save the physical file onto the harddisk however, I am doing:

    string filePath = Path.Combine(dirPath, item.Id + ".jpg");
    try
    {
        item.Image.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (Exception e)
    {
        Debug.WriteLine("DEBUG::LoadImages()::Error attempting to create image::" + e.Message);
    }
    

    and am getting an A generic error occurred in GDI+. I've checked the write permissions on the folder, and it does have write permissions. I'm unsure what is wrong here. I've also changed the ImageFormat to bmp and png and so forth to see if it was a conversion problem... but it isn't. Would anybody suggest something to try?

  • user1703401
    user1703401 over 11 years
    That still doesn't fix it, you must dispose tempImage to release the lock on the file. Use the using statement.
  • Magnum
    Magnum over 11 years
    @HansPassant thanks, I modified my answer to reflect your comment.
  • Richard Powell
    Richard Powell over 9 years
    Thank you very much! This is the reason I was having an issue. Creating a new Bitmap object in a using block solved this issue for me.