A generic error occurred in GDI+

Software

If you get this error in C# when you’re calling Bitmap.Save(path), it’s most likely a permissions error. Check your save path and make sure the calling app has permissions to save there.

UPDATE: I also got this error today when trying to save a Bitmap on top of an existing Bitmap. Not sure if the target bitmap was marked as read-only or not (I don”t think so).

Technorati Tags: ,

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Furl
  • Ma.gnolia
  • Reddit
  • TwitThis
7 Comments

7 Comments

  1. Karl Lilje  •  May 1, 2008 @1:12 am

    GDI+ keeps a lock on the file for the lifetime of the Bitmap,
    so if you load a file, and try and save it again, you will receive this error.
    (The reason is it decompresses on demand, which is a good thing for very large images or web images)

    To prevent the Generic Bitmap Error:
    Copy the Bitmap to a new bitmap, and Dispose the original Bitmap object after loading it. The file lock is now removed.

    Cheers
    Karl

  2. Leonardo  •  Sep 10, 2008 @3:41 am

    I’m cloning and disposing the bitmap but still can’t overwrite the file it was read from . Very sad.
    Thanks for your help
    Leo

  3. Greg  •  Oct 29, 2008 @5:19 pm

    Rather fed up with this right now – getting thoroughly inconsistent behaviour. Won’t even save an image newly created from scratch with no reference to any files…

    Not particularly impressed with the behaviour, but the typical MS ‘An error occurred but we won’t tell you what’ message is far from helpful!

  4. travis  •  Jan 16, 2010 @8:27 pm

    I am getting the same problem. This worked for me to get around it in the web page I am working on:

    using (System.Drawing.Image img = new Bitmap(galaxyBMP))
    {
    Response.Clear();
    Response.ContentType = “image/bmp”;

    using (MemoryStream stream = new MemoryStream())
    {
    img.Save(stream, ImageFormat.Bmp);
    stream.WriteTo(Response.OutputStream);
    }
    }

  5. Ian  •  Jan 18, 2010 @7:26 pm

    Ultimately you *will* hit problems trying to use GDI+ on a high volume web site or in an NT Service even if you’ve done the cloning workaround and numerous other defensive measures. The only totally reliable way to use it is in an isolated process that you kill off from time to time and restart and if it gives some random error like this for no good reason, just retry after launching a new worker process.

    See the cop-out clause here:-

    http://msdn.microsoft.com/en-us/library/system.drawing(VS.85).aspx

    Classes within the System.Drawing namespace are not supported for use within a Windows or ASP.NET service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.

  6. Farhad  •  Feb 6, 2010 @4:47 am

    lots of tnx
    my problem had been fix by your way
    GL

  7. Mauricio  •  Jul 26, 2010 @11:52 am

    Thanks a lot bro

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">