ASP.NET error on Bitmap.Save "Exception (0x80004005): A generic error occurred in GDI+."

From ASP Net - GDI+ and SAVE JPG or BMP on the server

99.9% of the time, when using GDI, 'a generic error occured' means that the directory you are trying to save to doesn't have the proper permissions. Typically, you need to make sure that the directory is allowing ASP.NET to modify files.

Did you check the permissions?


This error also occurs when you try to save on a network share. The only solution to fix this is to move your bitmap to a memory stream and save it with a file stream to the network share.

Here's an extension method to easily fix this:

public static void SaveOnNetworkShare(this System.Drawing.Image aImage, string aFilename, 
  System.Drawing.Imaging.ImageFormat aImageFormat)
{
  using (System.IO.MemoryStream lMemoryStream = new System.IO.MemoryStream())
  {
    aImage.Save(lMemoryStream, aImageFormat);

    using (System.IO.FileStream lFileStream = new System.IO.FileStream(aFilename, System.IO.FileMode.Create))
    {
      lMemoryStream.Position = 0;

      lMemoryStream.CopyTo(lFileStream);
    }
  }      
}