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

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).


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


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.