A generic error occurred in GDI+

By | April 8, 2006 | 46 Comments
Category: ASP .Net

Try the following code

Dim oBitmap As Bitmap
oBitmap = New Bitmap("c:\\example.jpg")
Dim oGraphic As Graphics
oGraphic = Graphics.FromImage(oBitmap)
Dim oBrush As New SolidBrush(Color.Black)
Dim ofont As New Font("Arial", 8 )
oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10)

You will get the above mentioned error:A generic error occurred in GDI+. This problem occurs because until the bitmap object is disposed, it creates a lock on the underlying image file. So you can save the newly generated file with different name but not overwrite the file because of lock. Now suppose you want to overwrite the file then create another bitmap from old bitmap. dispose the object of old bitmap, process new bitmap object and save the new bitmap object with original file name. The above chunk of code should be written in the following way.

Dim oBitmap As Bitmap
oBitmap = New Bitmap("c:\\example.jpg")
Dim oGraphic As Graphics
' Here create a new bitmap object of the same height and width of the image.
Dim bmpNew As Bitmap = New Bitmap(oBitmap.Width, oBitmap.Height)
oGraphic = Graphics.FromImage(bmpNew)
oGraphic.DrawImage(oBitmap, New Rectangle(0, 0, _
bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, _
oBitmap.Height, GraphicsUnit.Pixel)
' Release the lock on the image file. Of course,
' image from the image file is existing in Graphics object
oBitmap = bmpNew

Dim oBrush As New SolidBrush(Color.Black)
Dim ofont As New Font("Arial", 8 )
oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10)
oBitmap.Save("c:\\example.jpg", ImageFormat.Jpeg)

About Vishal Monpara

Vishal Monpara is a full stack Solution Developer/Architect with 13 years of experience primarily using Microsoft stack. He is currently working in Retail industry and moving 1's and 0's from geographically dispersed hard disks to geographically dispersed user leveraging geographically dispersed team members.

46 thoughts on “ A generic error occurred in GDI+

  1. Russell AliBey

    Here is a function that I created from an almagamation of all the posts I have seen on this issue. The image return should be persistable from ASP.Net. (it works for me) … (read: fine print, i’m not responsible for how you [mis]use my code).

    You supplied the final piece in the proper parameters to g.DrawImage(…)

    Thank you!


    Private Function GetImage(ByVal path As String) As Drawing.Bitmap
    Dim output As Drawing.Bitmap = Nothing

    Using img As Drawing.Bitmap = New Drawing.Bitmap(path)
    output = New Drawing.Bitmap(img.Width, img.Height)
    Using g As Drawing.Graphics = Drawing.Graphics.FromImage(output)
    g.DrawImage(img, New Drawing.Rectangle(0, 0, output.Width, output.Height), 0, 0, img.Width, img.Height, Drawing.GraphicsUnit.Pixel)
    End Using
    End Using

    Return output

    End Function

  2. Jerry Lees

    I use similar code to generate a Xbox 360 gamer signature on my site and when moving hosting providers it quit working.

    For those of you getting this error and this doesn’t work… check your permissions on the folder you are writing the image to… so that the user is able to WRITE to the folder.

    In my case the accounts needed more permissions on the new servers– and I had completely forgotten I did that as I wrote the code for the site.

  3. Suraj

    I have implemented following code to make the thumbnail of original image.
    This code is working perfect for one folder “Toys” as mentioned below but when i changed Folder from “Toys” to “Books” in same path it shows error “A generic error occured in gdi+”

    so what is the issue?
    Pl guide what is going wrong here.

    Dim objImage, objThumbnail As System.Drawing.Image
    Dim shtWidth, shtHeight As Short
    objImage = Drawing.Image.FromFile(“D:\My Documents\Pictures\”)
    shtWidth = 71
    shtHeight = 88
    objThumbnail = objImage.GetThumbnailImage(shtWidth, shtHeight, Nothing, System.IntPtr.Zero)
    Response.ContentType = “image/jpeg”
    ‘objThumbnail.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)

  4. Bizm

    (code contd)

    can u guide to solve this problem?

  5. Bizm

    i was searching for the solution of this error when i came across this page. i m getting an image from web cam and saving it on disk. however, sumtimes it runs just fine but sumtimes it gives this exception. This is the code;

    void CaptureImage()
    w = videoInfoHeader.BmiHeader.Width;
    h = videoInfoHeader.BmiHeader.Height;
    if (((w & 0x03) != 0) || (w < 32) || (w > 4096) || (h < 32) || (h > 4096))
    stride = w * 3;
    handle = GCHandle.Alloc(savedArray, GCHandleType.Pinned);
    scan0 = (int)handle.AddrOfPinnedObject();
    scan0 += (h – 1) * stride;
    b = new Bitmap(w, h, -stride, PixelFormat.Format24bppRgb, (IntPtr)scan0);
    savedArray = null;
    LivevideoBox.Image = b;

  6. Carlos Lozada


    You can also prevent locking an image file using a memory stream to save the image into it and then using a filestream to write the ouput. I Think it is simpler and cleaner. Check the following link:
    Vishal’s solution has helped me as well. Good job!

  7. sweavo

    great job, thanks! Who would have guessed that the load method holds a lock on the file … but that the lock would PREVENT save! crazy!

    This was driving me nuts but I was looking at my image buffers, not considering file locks. Thanks!

  8. hB

    FileStream() does not solve the same problem as:
    “if the stream was destroyed during the life of the Bitmap object, you cannot successfully access an image that was based on a stream. For example, the Graphics.DrawImage() function may not succeed after the stream has been destroyed.”

  9. Frush

    Forget what I just wrote, dunno why your exemple works fine with a small path but I have a hard time with “long” path

    for exemple try the same piece of code with

Leave a Reply

Your email address will not be published. Required fields are marked *