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.

23 Comments

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

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

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

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

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

  • Everytime I have run into it, it means
    "You used an invalid path to save a bitmap file."

  • 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!

  • 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

  • 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

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

  • 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?

  • Infragistics SUCKS!!!!

  • 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

  • when i save data this error is occurred.

  • 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);
    }
    }

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

  • 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!

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

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

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

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

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

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

Comments have been disabled for this content.