C# saving a bitmap results in generic GDI+ error

If you save a bitmap the following exception can pop up:

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)

Possible causes:

  • You tried to save the file with an invalid filename or in a directory that doesn’t exist
  • You constructed a bitmap from a memorystream that has been closed (the bitmap takes ownership of the MemoryStream and you are not allowed to close it yourself!)
  • You opened a bitmap from a file (-> which created a filestream) and you try to save another bitmap to the same file (-> doesn’t work because the file is blocked)

Leave a comment

Your email address will not be published. Required fields are marked *