Home > ASP .Net > Bitmap.save(): A generic error occurred in GDI+

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

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

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.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()
Categories: ASP .Net Tags:
  1. June 6th, 2006 at 04:07 | #1

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

  2. 9479r9r33n
    June 9th, 2006 at 08:25 | #2

    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

  3. September 21st, 2006 at 02:23 | #3

    gf

  4. jeff
    November 30th, 2006 at 02:11 | #4

    Thanks for the code, it helped me alot.

  5. Andy
    December 19th, 2006 at 04:23 | #5

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

  6. Cool Cow
    January 13th, 2007 at 05:37 | #6

    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.

  7. Nagendra
    January 26th, 2007 at 11:28 | #7

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

  8. Nagendra
    January 26th, 2007 at 11:29 | #8

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

  9. March 6th, 2007 at 01:42 | #9

    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.

  10. thierry
    May 18th, 2007 at 11:17 | #10

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

  11. Cam Dempster
    May 30th, 2007 at 11:56 | #11

    Keep on truckin dude!!

  12. Satyam
    June 7th, 2007 at 12:50 | #12

    I tried this….
    It did not help

  13. Geoff
    July 27th, 2007 at 04:21 | #13

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

  14. Geoff
    July 27th, 2007 at 04:22 | #14

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

    Cheers

  15. August 21st, 2007 at 11:56 | #15

    good solution

  16. August 24th, 2007 at 12:19 | #16

    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

  17. August 27th, 2007 at 08:23 | #17

    Abdul,

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

    Thanks

  18. zane
    August 28th, 2007 at 03:25 | #18

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

  19. Frush
    November 1st, 2007 at 07:35 | #19

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

  20. Frush
    November 1st, 2007 at 08:00 | #20

    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

  21. Frush
    November 1st, 2007 at 08:32 | #21

    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

  22. April 14th, 2008 at 11:09 | #22

    thanks for this one…

    it has me going for a while there.

  23. May 21st, 2008 at 02:39 | #23

    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

  24. sweavo
    May 22nd, 2008 at 04:40 | #24

    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!

  25. Carlos Lozada
    June 27th, 2008 at 01:15 | #25

    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!

  26. Bizm
    July 2nd, 2008 at 09:24 | #26

    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 & 0×03) != 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);

  27. Bizm
    July 2nd, 2008 at 10:51 | #27

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

    can u guide to solve this problem?

  28. Suraj
    August 20th, 2008 at 03:26 | #28

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

  29. September 3rd, 2008 at 06:28 | #29

    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.

  30. Russell AliBey
    September 11th, 2008 at 03:35 | #30

    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

  31. VISHAL
    October 1st, 2008 at 06:06 | #31

    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

  32. arpan shah
    April 11th, 2009 at 02:29 | #32

    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.

  33. Aqil
    April 27th, 2009 at 23:56 | #33

    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.

  34. goy gadol
    October 22nd, 2009 at 16:26 | #34

    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

  35. Amit Thaper
    February 13th, 2010 at 04:35 | #35

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

  36. Jeni Engels
    February 17th, 2010 at 16:36 | #36

    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

  37. Bob R
    March 7th, 2010 at 22:56 | #37

    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.

  38. Kunal
    April 28th, 2010 at 05:45 | #38

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

    }

    }

  39. EDUARDO
    May 25th, 2010 at 11:15 | #39

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

  40. July 14th, 2010 at 16:22 | #40

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

  1. No trackbacks yet.