Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

enter image description here

(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?
like image 516
Uwe Keim Avatar asked Mar 22 '13 13:03

Uwe Keim


People also ask

Could not save the PDF document a generic error occurred in GDI+?

GDI+ throws an error when it cannot save file. Following are 2 reasons why this error occurs. When you are initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file. Due to the lock when you try to save and overwrite your modified bitmap, it throws this error.


3 Answers

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.

saved image i2.jpg

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

like image 142
Uwe Keim Avatar answered Oct 05 '22 07:10

Uwe Keim


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

like image 39
Shadmehr Vadoodi Avatar answered Oct 05 '22 08:10

Shadmehr Vadoodi


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.

like image 27
Azarsa Avatar answered Oct 05 '22 07:10

Azarsa