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

Employee tired because of generic error occurred in GDI+

If you are trying to modify Bitmap, you may encounter the following GDI error which is very generic and does not provide any details. As the exception does not provide more details, it is very frustrating to figure out the root cause.

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

2 Reasons Why This Generic GDI Error Occurred

GDI+ throws an error when it cannot save file. Following are 2 reasons why this error occurs.

  • When you are initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file. Due to the lock when you try to save and overwrite your modified bitmap, it throws this error.
  • When you are saving a file, make sure the user has Write permission on the folder. This is important when you are getting this error on the Website because Website runs under restricted permissions.

3 Ways to Fix Generic GDI Error

There are three 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.
  • Make sure the folder in which you are trying to save file is writable. In Web Application, the application pool or account which runs the Website must have write access to to the folder in order to save the file. For example if you are running Website under “DefaultAppPool”, you must give “IIS AppPool\DefaultAppPool” user “write” access to the folder.

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)

As shown in the above example, I am reading the bitmap, modifying it and overwriting it on the same file. As the process creates a lock on the underlying image, it will throw an exception.

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

As shown in the above example, as soon as I create bitmap from an image, I am disposing the original bitmap. It releases the lock on the file. Hence I am able to overwrite the same file with updated bitmap.



53 responses to “Bitmap.Save(): A Generic Error Occurred in GDI+”

  1. Bitmap.Save() Invalid Parameter Used | Vishal Monpara

    […] my previous post of A Generic Error Occurred in GDI+, I explained about the lock which the Bitmap object creates on the file. Now if by mistake we […]

  2. ambrish Avatar


    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [ExternalException (0x80004005): A generic error occurred in GDI+.]
    System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +489937
    System.Drawing.Image.Save(String filename, ImageFormat format) +73
    DeathRegistration.getImageName(String imgName, FileUpload fuploder) +390
    DeathRegistration.btnRegistration_Click(Object sender, EventArgs e) +608
    System.EventHandler.Invoke(Object sender, EventArgs e) +0
    System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9796242
    System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +211
    System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12
    System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15
    System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1696

    1. Vishal Monpara Avatar
      Vishal Monpara

      Hello Ambrish,

      This blog post provides three ways to fix this error. Did you try those solutions?

      Vishal Monpara

      1. ambrish Avatar

        tried many times but same error issue….
        filling one government form, i think it’s their server issue.
        whats ur opinion on this

        1. Vishal Monpara Avatar
          Vishal Monpara

          Hello Ambrish,

          Make sure the Application Pool which is running the Website has “Write” permission to the folder in which you are trying to save file.

          Vishal Monpara

  3. Klaus Ruttkowski Avatar
    Klaus Ruttkowski

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

  4. Sandeep Arora Avatar
    Sandeep Arora

    Thanks for posting this. This really helped!

  5. Lakhan Dewangan Avatar
    Lakhan Dewangan

    Thanks This really Helped my in fixing my problem.

  6. Somanth Avatar

    Thanks for solution
    This is perfect solution for my problem

  7. Sajan 9895227675 Avatar
    Sajan 9895227675

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

  8. Paulo Tolentino Avatar
    Paulo Tolentino
  9. Paulo Tolentino Avatar
    Paulo Tolentino
  10. Tushar Avatar

    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 = Nothing
    fs = Nothing
    Return imgClone
    End Function

    Tushar 🙂

  11. Eric Duncan Avatar
    Eric Duncan

    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.ContentType = “image/png”;
    Response.BufferOutput = true;

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

  12. EDUARDO Avatar

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

  13. Kunal Avatar

    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]));
    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))));
    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++)
    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);
    //draw ‘the text on our image
    graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 40);
    bmpCanvas.Save(path, ImageFormat.Gif);//I got Here A generic error occurred in GDI+.
    return strText;

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



  14. Bob R Avatar
    Bob R

    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.

  15. Jeni Engels Avatar
    Jeni Engels

    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

  16. Amit Thaper Avatar
    Amit Thaper

    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

    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;
    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;
    return fn;

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

  17. goy gadol Avatar
    goy gadol

    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 = tempBmp
    End Sub

  18. Aqil Avatar

    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 );
    savedArray = null;
    Image old = pictureBox.Image;
    pictureBox.Image = b;
    stream = new MemoryStream();
    b.Save(stream, ImageFormat.Jpeg); // Getting error at this line.

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

  19. arpan shah Avatar
    arpan shah

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

    arpan shah.

  20. VISHAL Avatar

    Hello everyone

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


    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……


    any help apriciated ………………

    if found please reply ……….

  21. Russell AliBey Avatar
    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

  22. Jerry Lees Avatar
    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.

  23. Suraj Avatar

    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)

  24. Bizm Avatar

    (code contd)

    can u guide to solve this problem?

  25. Bizm Avatar

    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;

  26. Carlos Lozada Avatar
    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!

  27. sweavo Avatar

    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!

  28. hB Avatar

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

  29. jello Avatar

    thanks for this one…

    it has me going for a while there.

  30. Frush Avatar

    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

  31. Frush Avatar

    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

  32. Frush Avatar

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

  33. zane Avatar

    Great, this is just what I was looking for.

  34. Vishal Monpara Avatar
    Vishal Monpara


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


  35. abdul wakeel buneri Avatar
    abdul wakeel buneri

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

  36. sunil Avatar

    good solution

  37. Geoff Avatar

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


  38. Geoff Avatar

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

  39. Satyam Avatar

    I tried this….
    It did not help

  40. Cam Dempster Avatar
    Cam Dempster

    Keep on truckin dude!!

  41. thierry Avatar

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

  42. Phil Wilks Avatar
    Phil Wilks

    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.

  43. Nagendra Avatar

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

  44. Nagendra Avatar

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

  45. Cool Cow Avatar
    Cool Cow

    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.

  46. Andy Avatar

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

  47. jeff Avatar

    Thanks for the code, it helped me alot.

  48. ty Avatar


  49. 9479r9r33n Avatar

    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);
    Bitmap disposablebmp = new Bitmap(@”C:\temp\” + fileName);
    Bitmap newbmp = new Bitmap(disposablebmp,
    new System.Drawing.Size(Convert.ToInt32(disposablebmp.Width*.75),
    disposablebmp = newbmp;
    FileInfo fi = new FileInfo(@”C:\temp\” + fileName);
    x = Convert.ToInt32(fi.Length);

  50. Murray Ferguson Avatar
    Murray Ferguson

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

Leave a Reply

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