Bitmap.Save(): A Generic Error Occurred in GDI+

Category: ASP .Net

If you are trying to modify Bitmap, you may encounter the following GDI error which is very generic and does not provide any details.

Bitmap.Save(): A generic error occurred in GDI+

Why this generic GDI error occurred?

When you initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file. Due to lock when you try to save your modified bitmap, it throws this error.

How do I fix this generic GDI error?

There are two ways to fix this issue

  • Instead of overwriting the file, save a new file with a different name than the original file
  • Only when the Bitmap object is disposed, the underlying lock on the file is removed. Once the lock is removed, you may overwrite the file. If you must overwrite the existing file, create a separate bitmap object from existing bitmap object. Now dispose the old bitmap object which will release the lock on the image file. Go ahead and make the needed changes in new bitmap object and save the new bitmap object with original image file name.

Sample code that causes error

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)
oBitmap.Save("c:\\example.jpg",ImageFormat.Jpeg)
oBitmap.Dispose()
oGraphic.Dispose()

Sample code with fix

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.Dispose()
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)
oGraphic.Dispose()
ofont.Dispose()
oBrush.Dispose()
oBitmap.Save("c:\\example.jpg", ImageFormat.Jpeg)
oBitmap.Dispose()

Share

48 comments

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

  1. Klaus Ruttkowski says:

    I have been looking for a solution to this problem. This has helped. Thank you for your contribution.
    Greeting Klaus

  2. Sandeep Arora says:

    Thanks for posting this. This really helped!

  3. Lakhan Dewangan says:

    Thanks This really Helped my in fixing my problem.

  4. Somanth says:

    Thanks for solution
    This is perfect solution for my problem

  5. Sajan 9895227675 says:

    no need of all these…after dispose just add GC.Collect(); …tats all

  6. Tushar says:

    This is the final solution ( VB .NET). Hope this ends the problems of bitlocks and Image Saving.

    Public Shared Function CopyToStandaloneBitmap(ByRef InputImage As Image) As Image

    Dim memory As New MemoryStream()
    InputImage.Save(memory, Imaging.ImageFormat.Png)

    Return Image.FromStream(memory)
    End Function

    Public Shared Function InitializeStandaloneImageCopy(ByVal strPathFile As String) As Image

    If strPathFile Is Nothing Then Return Nothing
    If strPathFile.Length <= 0 Then Return Nothing
    If Not FileExists(strPathFile) Then Return Nothing

    Dim fs As New FileStream(strPathFile, FileMode.Open, FileAccess.Read)
    Dim img As Image = Image.FromStream(fs)
    Dim imgClone As Image = CopyToStandaloneBitmap(img)
    img.Dispose()
    img = Nothing
    fs.Close()
    fs = Nothing
    Return imgClone
    End Function

    *******************************
    Tushar 🙂

  7. Eric Duncan says:

    Actually, the fix is to properly dispose of your objects in order. In C#, I use the using() syntax, which calls Dispose() for me at the end of the scope. So, the fix would be to end your scope before calling Save() on the bitmap.

    // new image with transparent Alpha layer
    using (var bitmap = new Bitmap(330, 18, PixelFormat.Format32bppArgb))
    {
    using (var graphics = Graphics.FromImage(bitmap))
    {
    // add some anti-aliasing
    graphics.SmoothingMode = SmoothingMode.AntiAlias;

    using (var font = new Font(“Arial”, 14.0f, GraphicsUnit.Pixel))
    {
    using (var brush = new SolidBrush(Color.White))
    {
    // draw it
    graphics.DrawString(user.Email, font, brush, 0, 0);
    }
    }
    }

    // setup the response
    Response.Clear();
    Response.ContentType = “image/png”;
    Response.BufferOutput = true;

    // write it to the output stream
    bitmap.Save(Response.OutputStream, ImageFormat.Png);
    Response.Flush();
    }

  8. EDUARDO says:

    muy buen post amigo
    muy buena solucion y un plus mas es que reduce el tamaño de las imagenes
    sin perder calidad

  9. Kunal says:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    string strPathToImage = “captchaimg/captcha.gif”;
    string strText = fncDrawCaptcha(Server.MapPath(strPathToImage));
    imgCaptcha.ImageUrl = strPathToImage;
    Session[“strText”] = strText;

    }

    }
    public string fncDrawCaptcha(string path)
    {

    int[] BackgroundColor = new int[] { 255, 255, 255 };
    bool RandomBackgroundNoiseColor = true;
    bool RandomTextColor = true;
    int[] BackgroundNoiseColor = new int[] { 150, 150, 150 };
    int[] TextColor = new int[] { 200, 200, 200 };
    HatchStyle BackgroundNoiseTexture = HatchStyle.Min;
    int length = 6;
    int height = 100;

    int width = 200;
    width = width + ((length – 6) * 30);
    Random ranRotate = new Random();
    string strText = System.Guid.NewGuid().ToString();
    strText = strText.Replace(“-“, String.Empty);
    strText = strText.Substring(0, length);

    Bitmap bmpCanvas = new Bitmap(width, height, PixelFormat.Format24bppRgb);
    Graphics graCanvas = Graphics.FromImage(bmpCanvas);
    RectangleF recF = new RectangleF(0, 0, width, height);
    Brush bruBackground = default(Brush);
    SolidBrush letterBrush = default(SolidBrush);

    graCanvas.TextRenderingHint = TextRenderingHint.AntiAlias;

    if (RandomBackgroundNoiseColor == true)
    {
    bruBackground = new HatchBrush(BackgroundNoiseTexture, Color.FromArgb((ranRotate.Next(0, 255)), (ranRotate.Next(0, 255)), (ranRotate.Next(0, 255))), Color.FromArgb(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2]));
    }
    else
    {
    bruBackground = new HatchBrush(BackgroundNoiseTexture, Color.FromArgb(BackgroundNoiseColor[0], BackgroundNoiseColor[1], BackgroundNoiseColor[2]), Color.FromArgb(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2]));
    }

    graCanvas.FillRectangle(bruBackground, recF);

    if (RandomTextColor == true)
    {
    letterBrush = new SolidBrush(Color.FromArgb((ranRotate.Next(0, 255)), (ranRotate.Next(0, 255)), (ranRotate.Next(0, 255))));
    }
    else
    {
    letterBrush = new SolidBrush(Color.FromArgb(TextColor[0], TextColor[1], TextColor[2]));
    }
    System.Drawing.Drawing2D.Matrix matRotate = new System.Drawing.Drawing2D.Matrix();
    int i = 0;
    for (i = 0; i <= strText.Length – 1; i++)
    {
    matRotate.Reset();
    int intChars = strText.Length;
    int x = width / (intChars + 1) * i;
    int y = height / 2;
    //matRotate.RotateAt(ranRotate.Next(-30, 30), new PointF(width / (intChars + 1) * i, height * 0.5) );
    matRotate.RotateAt(ranRotate.Next(-30, 30), new PointF(x, y));
    graCanvas.Transform = matRotate;
    if (i == 0)
    {
    //draw ‘the text on our image
    //graCanvas.DrawString(strText.Chars(i), new Font("Comic Sans MS", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, height * 0.4);
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Comic Sans MS", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
    }
    else if (i == 1)
    {
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Arial", 30, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
    }
    else if (i == 2)
    {
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Times New Roman", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
    }
    else if (i == 3)
    {
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Georgia", 35, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
    }
    else if (i == 4)
    {
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
    }
    else if (i == 5)
    {
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Geneva", 30, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
    }
    else
    {
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
    }
    graCanvas.ResetTransform();
    }
    bmpCanvas.Save(path, ImageFormat.Gif);//I got Here A generic error occurred in GDI+.
    graCanvas.Dispose();
    bmpCanvas.Dispose();
    return strText;

    }
    protected void subCheckCaptcha(object sender, EventArgs e)
    {
    if (!(txtCaptcha.Text == Session["strText"].ToString()))
    {
    lbl_Msg.Text = "The characters does not match";
    }
    else
    {
    lbl_Msg.Text = "";
    Response.Write("alert(‘Captcha text entered successfully’)”);

    }

    }

  10. Bob R says:

    Thanks Jerry Lees. My app was working fine in my development environment, but failed with “A generic error occurred in GDI+. ” whenever I tried to run the published version on the Web server. Gave IIS_USRS write permissions on the folder and all is now good.

  11. Jeni Engels says:

    Thanks very much – this code was extremely helpful in solving a problem that has been bugging me for a very long time. The way it was written and explained way super. Thanks again

  12. Amit Thaper says:

    Here Is the Sample Code which will be Solved Some Error. By Using this we can not get an Errror Of GDI+ in local host but when we Uploaded into the Web Server it can’t Gives Same error

    [WebMethod]
    public void SaveImage(String ImageName, Byte[] ByteArray, Int32 Catid, Int32 UserId)
    {
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = “Insert tbl_ImgSave values(@nam,@catid,@usrid,@imgrate)”;
    cmd.Parameters.Add(“@nam”, System.Data.SqlDbType.VarChar, 50).Value = SaveToFolder(ByteArray, ImageName);
    cmd.Parameters.Add(“@catid”, System.Data.SqlDbType.Int).Value = Catid;
    cmd.Parameters.Add(“@usrid”, System.Data.SqlDbType.Int).Value = UserId;
    cmd.Parameters.Add(“@imgrate”, System.Data.SqlDbType.Int).Value = 0;
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    con.Close();
    }
    [WebMethod]
    public String SaveToFolder(Byte[] armd, String ImageName)
    {
    String fn = Guid.NewGuid().ToString() + ImageName.Substring(0) + “.jpg”;

    Image newImage = ByteArrayToImage(armd);
    //String fn = ImageName + “.jpg”;
    String sd = Server.MapPath(“Images”);
    if (sd.EndsWith(“\\”) == false)
    {
    sd += “\\”;
    }
    sd += fn;
    newImage.Save(sd);
    return fn;

    }
    public Image ByteArrayToImage(Byte[] ByteArray)
    {
    MemoryStream ms = new MemoryStream(ByteArray);
    Image ReturnImage = Image.FromStream(ms);
    return ReturnImage;
    }

  13. goy gadol says:

    I’m still getting the error

    Below is my code,any help would be great

    Private Sub savebitmap()
    Dim strFile As String = Application.StartupPath + “\” + Patid.ToString + “.png”
    Dim originalImage As Image = Image.FromFile(strFile)
    Dim tempBmp As New Bitmap(originalImage.Width, originalImage.Height)
    Dim g As Graphics = Graphics.FromImage(originalImage)
    g.DrawImage(tempBmp, 0, 0, originalImage.Width, originalImage.Height)
    originalImage.Dispose()
    originalImage = tempBmp
    g.Dispose()
    originalImage.Save(strFile)
    End Sub

  14. Aqil says:

    Hi I m getting error-a generic error occurred in gdi+ while saving an image to stream

    Bitmap b = new Bitmap( w, h, -stride, PixelFormat.Format24bppRgb, (IntPtr) scan0 );
    handle.Free();
    savedArray = null;
    Image old = pictureBox.Image;
    pictureBox.Image = b;
    stream = new MemoryStream();
    b.Save(stream, ImageFormat.Jpeg); // Getting error at this line.
    b.Dispose();

    I m unable to solve this error.
    Any suggestions would be greatly appreciated.

  15. arpan shah says:

    hey man thank a lot bro……u really saved my day..i search a lot on this and found many things but ur solution works for me excelent…

    again thx ….

    regards,
    arpan shah.

  16. VISHAL says:

    Hello everyone

    this is very nice code ……………………………

    —————————————————–
    BUT IT NOT SOLVED MY PROBLEM
    —————————————————–

    My Problem is i have a picture box and that picturebox i draw a multiple image ……
    and now i want to save that all image in one
    jpg or another format of image so……

    HOW CAN I STORE THE ALL IMAGES TO A PERTICULAR IMAGE ………………..

    any help apriciated ………………

    if found please reply ……….

    onlyvish4me@gmail.com

  17. Russell AliBey says:

    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!

    -R

    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

  18. Jerry Lees says:

    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 asp.net/Iuser accounts needed more permissions on the new servers– and I had completely forgotten I did that as I wrote the code for the site.

  19. Suraj says:

    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)
    objThumbnail.Save(“C:\inetpub\wwwroot\Toys\thumbnails\Pics1.jpg”)
    objImage.Dispose()
    objThumbnail.Dispose()

  20. Bizm says:

    (code contd)
    ………
    b.Save(“C:\\abc.jpg”,System.Drawing.Imaging.ImageFormat.Jpeg);
    b.dispose();
    }

    can u guide to solve this problem?

  21. Bizm says:

    hye,
    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()
    {
    try
    {
    w = videoInfoHeader.BmiHeader.Width;
    h = videoInfoHeader.BmiHeader.Height;
    if (((w & 0x03) != 0) || (w < 32) || (w > 4096) || (h < 32) || (h > 4096))
    return;
    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);
    handle.Free();
    savedArray = null;
    LivevideoBox.Image = b;
    b.Save(“C:\\abc.jpg”,System.Drawing.Imaging.ImageFormat.Jpeg);

  22. Carlos Lozada says:

    Hi,

    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:
    http://aspalliance.com/319
    Vishal’s solution has helped me as well. Good job!

  23. sweavo says:

    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!

  24. hB says:

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

    http://support.microsoft.com/?id=814675

  25. jello says:

    thanks for this one…

    it has me going for a while there.

  26. Frush says:

    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
    %temp%\ImageConvertion\exemple.jpg

  27. Frush says:

    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
    %temp%\ImageConvertion\exemple.jpg

  28. Frush says:

    Might help some ppl I think someone mention it but the key for me was to use the @#% “\\” for folder seperator instead of “\”

  29. zane says:

    Great, this is just what I was looking for.
    Thanks

  30. Abdul,

    I did not understand your question. I may be able to help you if you can elaborate on the issue.

    Thanks

  31. abdul wakeel buneri says:

    how can i include the file info in vb.net
    when i include the file info the compiler give me an error
    please tell me the solution

  32. sunil says:

    good solution

  33. Geoff says:

    Opps
    image = new Image(fs);
    should be
    image = Image.FromStream(fs);

    Cheers

  34. Geoff says:

    I prefer this method because it never locks the original file…
    Image image = null;

    using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
    {
    image = new Image(fs);
    }

  35. Satyam says:

    I tried this….
    It did not help

  36. Cam Dempster says:

    Keep on truckin dude!!

  37. thierry says:

    your are a star!!! i love you, i was about to shoot somebody… ;-))))))

  38. Phil Wilks says:

    Thanks for this – I’ve been trying to get to the bottom of this problem for some time, and didn’t think of the file being locked.

  39. Nagendra says:

    I had used the same code, but still getting the same error when saving the bitmap image in to the folder

  40. Nagendra says:

    I had used the same code, but still getting the same error when saving the bitmap image in to the folder

  41. Cool Cow says:

    You might want to add to your post you always need to use absolute paths in windows forms when working with images.

    In windows forms it isn’t necessary to use abolute path when working with files / directorys
    eg: IO.Directory.Create(“test”) will create a dir named test in the dir where your application is.

    For images you can’t do that.
    Now an easy way to fix that, even if you dont know if the path is relative or not:

    filename = new IO.FileInfo(filename).FullName

    This will always create the correct absolute path, even if filename is already absolute.

  42. Andy says:

    Hi,
    Thanks for the post, but how come that it didn’t save the new Gif file?

  43. jeff says:

    Thanks for the code, it helped me alot.

  44. ty says:

    gf

  45. 9479r9r33n says:

    Thanks for your post. It gave me a template to create a method that looks at an image and coninues to take 25% off of it until it reaches a predetermined file size:

    if((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
    {
    int x = Convert.ToInt32(File1.PostedFile.InputStream.Length);//100009;
    string fileName = Path.GetFileName(File1.PostedFile.FileName);
    File1.PostedFile.SaveAs(@”C:\temp\” + fileName);
    File1.Dispose();
    while(x>100000)
    {
    Bitmap disposablebmp = new Bitmap(@”C:\temp\” + fileName);
    Bitmap newbmp = new Bitmap(disposablebmp,
    new System.Drawing.Size(Convert.ToInt32(disposablebmp.Width*.75),
    Convert.ToInt32(disposablebmp.Height*.75)));
    disposablebmp.Dispose();
    disposablebmp = newbmp;
    FileInfo fi = new FileInfo(@”C:\temp\” + fileName);
    x = Convert.ToInt32(fi.Length);
    fi.Delete

  46. Murray Ferguson says:

    Many thanks for the fix.
    Had been struggling with this issue for quite some time. I very grateful.
    Murray