ExternalException (0x80004005) A generic error occurred in GDI+


I have a problem with my program. I read byte[] from a SQL Server database and save this as a JPEG image.

while (reader1.Read())
    picBytes = null;
    picBytes = (byte[])reader1.GetValue(0);
    if (picBytes != null)
            ms = new MemoryStream(picBytes, 0, picBytes.Length);
            returnImage = Image.FromStream(ms, true);
            returnImage.Save(@"path" + array[2] + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        catch (Exception e)
            Console.WriteLine(e + array[2] + ".jpg");

I have read that in 99% there are permissions errors but 500 images are created correctly and 50 are not.

And this error is only thrown under Windows 7 64Bit... with XP there are no problems:

Error in System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
asked on Stack Overflow Mar 27, 2013 by Tiega • edited Mar 28, 2013 by vivat pisces

3 Answers


There are still a few who have the same problem, so here how i solve the problem:

  • Screw System.Drawing


  • File.WriteAllBytes(pathString, picBytes);
answered on Stack Overflow Jul 31, 2014 by Tiega

I had a similar problem, where the same code worked for most images, and produced the error for some. As a workaround I found that creating a Bitmap from the loaded image and saving the bitmap works for all images. In your case this would mean:

using(var bmp=new Bitmap(returnImage))
  bmp.Save(@"path" + array[2] + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

I have no idea why this works, so I would be very interested in any further information.

EDIT: After some further investigation it seems that the issue (at least in my case) is with the EXIF data in the images. If the length of the EXIF block is bigger than 0xEFFF (i.e the first byte is 0xFF) the image cannot be saved. So stripping the EXIF block from the beginning of the file also solves the problem

answered on Stack Overflow Mar 27, 2013 by csgero • edited Mar 28, 2013 by csgero

I had this error appear when I ran my installed program, but not when running it in the Visual Studio debugger. The error code 0x80004005 indicates a permission error. I suspect Windows 7 is more particular than XP about read/write permissions in certain circumstances, and Windows 10 is definitely more restrictive. I was able to resolve my problem by running the program as administrator. However, that's bad practice for a program I intend to distribute, so I examined the code and found that I was trying to write a temporary emf file in the program folder, which is protected in Windows 10. I could solve that by writing to the unprotected ProgramData folder for that program or preferably, using System.IO.Path.GetTempPath to get the default path name for temp files. Using a memory stream is an even better solution.

answered on Stack Overflow May 14, 2020 by Robert Cody • edited May 15, 2020 by Robert Cody

User contributions licensed under CC BY-SA 3.0