How to find reason for Generic GDI+ error when saving an image?

a generic error occurred in gdi+ windows 10
a generic error occurred in gdi+ means
a generic error occurred in gdi+ windows 7
a generic error occurred in gdi+ blue prism
a generic error occurred in gdi+ automation anywhere
unhandled exception gdi+
cherwell a generic error occurred in gdi+
a generic error occurred in gdi+ system drawing sql server

Having a code that works for ages when loading and storing images, I discovered that I have one single image that breaks this code:

const string i1Path = @"c:\my\i1.jpg";
const string i2Path = @"c:\my\i2.jpg";

var i = Image.FromFile(i1Path);
i.Save(i2Path, ImageFormat.Jpeg);

The exception is:

System.Runtime.InteropServices.ExternalException occurred

A generic error occurred in GDI+.

at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at ...

As far as I can see, there is nothing special about the image. It is approx 250 pixels in size and can be opened in e.g. Windows Image Viewer or Paint.NET:

(Since the image above, after being uploaded to Stack Overflow does not produce the error anymore, I've put the original image here)

What I discovered is that upon calling the Save method, the destination image file is being created with zero bytes.

I am really clueless on what causes the error.

My questions:

  • Can you think of any special thing that would hinder .NET from saving the image?
  • Is there any way (beside panicing) to narrow down these kind of errors?

While I still did not find out the reason what exactly caused the error when saving the image, I found a workaround to apply:

const string i1Path = @"c:\my\i1.jpg";
const string i2Path = @"c:\my\i2.jpg";

var i = Image.FromFile(i1Path);

var i2 = new Bitmap(i);
i2.Save(i2Path, ImageFormat.Jpeg);

I.e. by copying the image internally into a Bitmap instance and saving this image instead of the original image, the error disappeared.

I'm assuming that by copying it, the erroneous parts the caused the original Save call to fail are being removed an/or normalized, thus enabling the save operation to succeed.

Interestingly, the so stored image has a smaller file on disk (16 kB) than its original source (26 kB).

How to find reason for Generic GDI error when saving an image , A generic error occurred in GDI . at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.  GDI = Wages + Profits + Interest Income + Rental Income + Taxes − Subsidies on Production and Imports + Statistical Adjustments GDP = Consumption + Investment + Government Purchases

First of all make sure, that the desired folder has Read/Write permissions. Changing the permissions solved this problem for me.

Anas Ghanem Blog, Solving "A generic error occurred in GDI+" exception. Image from the memory stream to the file system , you may get that error. I know that there is many other causes of that problem,but that's what i know until this time. FromFile solved the GDI+ problem, as well as preventing a lock on the image file Thankyou for taking the trouble to answer an old post. I have exactly the same line of code but in c#. I'll try your soloution and report back. I am a realtive newcomer to C# 2 years as opposed to 22 plain C. Micorsoft help is inpenitrable, you have to know how c# works before you can find out how it works.

Solution is here, you must dispose image object to release the memory on the server. Try use using statement. Make sure destination directory on server exists too.

The Weekly Source Code 50, The error was "A generic error occurred in GDI+" and you know that if an ALL reasons you're getting "a generic error occurred in GDI+" but it can answer mine. //some stuff snipped, get the params from the QueryString. When I boot up PE 2018 running on a NEW [February 16, 2018] Dell XPS 27 using Windows 10 64Bits I get a message alerting me to use a better display driver than Microsoft's 'GDI generic 1.1.0'. On February 18th I had a minor issue and while on with MS Support asked the MS Tech to upgrade/check my Image drivers, per the pop-up.

The reason may be that the image is loaded lazily and the loading process is not yet finished when you try to save it.

Following what's said in this blog post (assuming you're German by the picture you linked in your question) provides a possible solution. Also this SO question's accepted answer indicates this is due to the fact the image file you're trying to save to is locked.

EDIT For Ulysses Alves, from the linked blog entry: If you load an image using Image.FromFile() it remains locked until it is disposed of. This prevents calls to Save().

pictureBox1.Image = Image.FromFile("C:\\test\\test1.jpg");
pictureBox1.Image.Save("C:\\test\\test2.jpg");

The above code throws an error.

To make it work, you need to copy the image. The following code works:

pictureBox1.Image = Image.FromFile("C:\\test\\test1.jpg");
Image copy = pictureBox1.Image;
copy.Save("C:\\test\\test2.jpg")

GDI+ Programming in C# and VB .NET, Dispose() End Sub I advised you earlier to remember the generic fonts. a bad argument, I can safely assume GDI+ could not find the fontfor whatever reason. It then details the support that GDI provides to graphics drivers. References to GDI in this section are implicit references to kernel-mode GDI; Microsoft Win32 GDI will be explicitly identified. Kernel-mode GDI is also known as the Graphics Engine. GDI function and structure references are documented in the Display Devices Reference section.

Article: "A generic error occurred in GDI+" error when uploading png , Republish the problematic image/images and regenerate their thumbnails. Second Workaround: 1. Check what the Application Pool Identity in IIS  Had a very similar problem and also tried cloning the image which doesn't work. I found that the best solution was to create a new Bitmap object from the image that was loaded from the memory stream.

Windows Graphics Programming: Win32 GDI and DirectDraw, There are three types of bitmaps supported by GDI—which one should you use to re- duce ofapplications on a system; when system resources are low, how can I find the troublemaker? But the most generic GDI object type is HGDIOBJ. The computer does not have enough hardware resources to cope with the opening of the GDI file. Drivers of equipment used by the computer to open a GDI file are out of date. If you are sure that all of these reasons do not exist in your case (or have already been eliminated), the GDI file should operate with your programs without any problem.

[Solved] A generic error occurred in GDI+, How come you did not report the problem properly? Run it under debugger and see in what line you got an exception. Catch all exceptions and  The topics in this section describe Graphics Device Interface (GDI) and its relationship to printer drivers and display adapter drivers. Display adapter drivers that run on Windows Vista can adhere to one of two models: the Windows Vista display driver model or the Windows 2000 display driver model.

C# Bitmap Save Generic GDI+ Error, tons of info on this on the web, but that's the core of all the info you'll find. Bugs in GDI+ cause it to hold a file open, unless you're very careful  The reason may be that the image is loaded lazily and the loading process is not yet finished when you try to save it. Following what's said in this blog post (assuming you're German by the picture you linked in your question) provides a possible solution.

Comments
  • MSDN says a Bitmap loaded from a Stream requires the Stream to remain in existence as long as the Bitmap exists.
  • Unreleated to your problem: But the same exception occoures if the path does not exist (took me a while to figure out I had a typo). Might help someone else.
  • This worked for me as well. I had to force garbage collection afterwards, on account of the 'out of memory exception' that was caused.
  • Worked for me too - no idea why. Only had the issue with jpg images (png etc were fine)
  • I had this issue with .bmp, .jpg, and .png but I wasn't loading a file, I was creating it in memory and trying to save it. CAUSE: the original bitmap is tied to the stream it loaded from (FileStream or MemoryStream). The cloned bitmap it not tied to a stream, as it copied the pixel bytes. It might also have worked to Freeze the original bitmap before saving it.
  • That's like 90% of the cases :-)
  • This sounds like a really bad answer to me.
  • "Make sure destination directory on server exists" was my issue
  • @UweKeim - a using on a temporary IDisposable object just ensures the GC is called for it as soon as it is safe to do so.
  • "Make sure destination directory on server exists" was my issue as well
  • "Make sure destination directory on server exists" was my issue as well. So easy to overlook.