Solving "A generic error occurred in GDI+" exception.

 

Hi,

If you are getting that error , then I can say that your application doesn't have a write permission on some directory.

For example, if you are trying to save the Image from the memory stream to the file system , you may get that error.

I also faced that error when I was using Infragistics charts control , the control was trying to create a temporary chart in ChartImages folder and my application was not given the write permission.

Please if you are using XP, make sure to add write permission for the aspnet account on that folder.

If you are using windows server (2003,2008) or Vista, make sure that add write permission for the Network service account.

Hope it help some one.

Published Saturday, February 28, 2009 9:23 PM by anas
Filed under: , ,

Comments

# re: Solving "A generic error occurred in GDI+" exception.

Saturday, February 28, 2009 3:44 PM by Uwe

Na, _one_ of the possible errors could be the one, you described.

The "Generic error" in fact is used in certain scenarios. It means "we were to lazy to write better error return code".

This really sucks.

# re: Solving "A generic error occurred in GDI+" exception.

Saturday, February 28, 2009 3:57 PM by anas

Well, at least that's based on my experience and on the problems i faced.

I know that there is many other causes of that problem,but that's what i know until this time.

# re: Solving "A generic error occurred in GDI+" exception.

Saturday, February 28, 2009 4:18 PM by SGWellens

I think it's the most common error so permissions should always be checked first.

# re: Solving "A generic error occurred in GDI+" exception.

Sunday, March 1, 2009 5:54 AM by peSHIr

It might not be that you don't have write permissions, but a sharing violation might be in effect.

Common scenario that this happens:

1) Read an image into a Bitmap object

2) Manipulate the bitmap

3) Wanting to write back the bitmap to overwrite the original image file you read in step 1.

This can happen because the Bitmap implementation seems to be as lazy as possible. It can choose the just remember the path that was the source of the image bits (or even a handle to the opened file they came from). This then means your software has the original image file open, causing this problem when you try to overwrite it in step three.

To work around this use some simple code in an intermediate step (let's say 1a) that creates a new, blank Bitmap object with the same width/height/colordepth as the image you read. Then, get a Graphics object for the blank image and draw (BitBlt, basically) the loaded image onto the blank image. Then, dispose the loaded image. This gets you a Bitmap object that contains the content of the loaded image actually stored in memory, without any link to the original image file. Then, work on the Bitmap the way you like and you can easily save it back over the original file in step 3 without sharing violations. (If you have write permissions, of course. ;-))

# re: Solving "A generic error occurred in GDI+" exception.

Monday, March 2, 2009 3:08 AM by anas

Hi peSHIr,

Thanks for information! I really appreciate it and hope it will help someone in the future.

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, March 19, 2009 3:43 PM by plan17b

Everytime I have run into it, it means

"You used an invalid path to save a bitmap file."

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, May 7, 2009 2:26 PM by Mike

Of course that was it! Why wouldn't it be? For your reference, I've written a nice bit of GDI code to make address labels, and we were porting it to ASP. Got the dreaded "generic error" in GDI+ when I tried to save to the webdir using a Bitmap class derived from the GDI hBitmap. Of course, ASP docs were of zero help, but your page got me up and running right away. I probably should've discovered it myself, but I am an API applications programmer, and it simply never occurred to me that it would be that easy!

T H A N K S  H E A P S  Anas!

# re: Solving "A generic error occurred in GDI+" exception.

Sunday, August 2, 2009 3:11 PM by Phil

Thanks for the advice!  I never would have thought such a 'general' error had to do with a filesystem issue!  The engineer who came up with that error message deserves a demotion! :)

Thanks,

PHil

# re: Solving "A generic error occurred in GDI+" exception.

Tuesday, October 13, 2009 10:44 AM by Matías Ram

I have the same problem, GDI+ exception.

But i resolve, when change the property visible to false from the char.

Later i will fill the grafic from the page with a dataset.

I hope for with solucion resolve your problem

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, October 29, 2009 10:47 AM by SunandaSh

I have this error while using AlternateView and LinkedResource for taking an image and using it in the mail and sending it using System.net.mail. Since i am using two images I am making a collection of linkedResource -

Dim collection As LinkedResourceCollection = hmtlView.LinkedResources()

               collection.Insert(0, background)

               collection.Insert(1, EmpImage)

               hmtlView.LinkedResources.Concat(collection)

               mailMessage.AlternateViews.Add(hmtlView)

I get this exception here - A generic error occurred in GDI+

Please help me with your suggestions.

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, March 18, 2010 1:47 PM by MARK

Hello:

May anyone help on this?

I am having the same exception ("A generic error occurred in GDI+").

Yet i am not running an asp but a windows form application, it basically takes a picture form a camera installed in the pc and stores it in a network folder.

Now this is what its strange, the application runs well in all the pcs of the network but one, that throws the exception.

Still it is not a permission issue, because i checked that the folder has the everyone in full control so...

Are there any other causes for these to appear more based on hardware then other thing?

# re: Solving "A generic error occurred in GDI+" exception.

Saturday, March 27, 2010 10:09 PM by guru

Infragistics SUCKS!!!!

# re: Solving "A generic error occurred in GDI+" exception.

Friday, May 7, 2010 11:34 PM by Srikumar Srinivasan

Hi,

I had the same problem with windows 2008, Issue Fixed when I changed the Authentication -> anonumous Authentication property to use the Application Pool Identity.

Regards

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, October 7, 2010 5:09 AM by asir Akbar

when i save data this error is occurred.

# re: Solving "A generic error occurred in GDI+" exception.

Friday, March 18, 2011 6:19 AM by Maarten Nieber

In my case, this replacement for Image.FromFile solved the GDI+ problem, as well as preventing a lock on the image file

public static Image FromFile(string path)

{

using (var fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))

{

Image result = System.Drawing.Image.FromStream(fs);

fs.Close();

// We MUST call the constructor here,

// otherwise the bitmap will still be linked to the original file

return new Bitmap(result);

}

}

# re: Solving "A generic error occurred in GDI+" exception.

Monday, March 28, 2011 2:26 AM by Dan

This is a wierd problem. I fixed it by impersionating a valid user. Seems to me that it doesn't matter what user account i used.

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, May 5, 2011 2:19 AM by David Grinberg

I was getting this error when trying to access the image, after accidentally closing the file stream.

Make sure the file stream stays open, while you are accessing the image!

# re: Solving "A generic error occurred in GDI+" exception.

Wednesday, May 25, 2011 5:17 AM by Ovini

I got this error since I didn't dispose the "LinkedResource" objects I used.

# re: Solving "A generic error occurred in GDI+" exception.

Thursday, June 16, 2011 3:53 AM by Nitin Tyagi

I am uploading a PDF file. It was working fine but all of a sudden it is displaying this error now. I corrected the folder's path but still it's giving the error. I don't know what to do.

# re: Solving "A generic error occurred in GDI+" exception.

Tuesday, July 12, 2011 8:44 AM by Sandeep

Exellant man.. you solved my problem..!! :)

# re: Solving "A generic error occurred in GDI+" exception.

Tuesday, October 11, 2011 1:35 AM by dseven

Perfect. Straight to the point. I just read the first line, Voila, it solved my problem. Thanks a lot.

# re: Solving "A generic error occurred in GDI+" exception.

Tuesday, February 21, 2012 1:27 AM by Muhammad Idrees

Thanks, great post, I really facing this exception and after lot of googling I found this by some reference. Perfect solution.

# re: Solving "A generic error occurred in GDI+" exception.

Friday, May 11, 2012 8:52 AM by Steve Borman

I was having the same problem with the line

bitmap.Save(ms,ImageFormat.Png)

but only on the server and not in code (This a web application).

No permissions issues as ms is a MemoryStream.

No idea why but simply changing the code to bitmap.Save(ms,ImageFormat.Gif) solved the problme

Hope this helps.