locked
ThumbImage.Save() -- A generic error occurred in GDI+ RRS feed

  • Question

  • User-739221493 posted

    Hi,

    I am getting this error when trying to save a newly created thumbnail back to the server.  Here's my code:

            Dim fullSizeImg As System.Drawing.Image, ThumbNailImage As System.Drawing.Image
            Dim dummyCallBack As System.Drawing.Image.GetThumbnailImageAbort
            dummyCallBack = New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
            fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(ImageUrl))
            ThumbNailImage = fullSizeImg.GetThumbnailImage(100, 100, dummyCallBack, IntPtr.Zero)
            ThumbNailImage.Save(ThumbURL, ImageFormat.Jpeg)

    I get the error at the .Save line.  I have checked permissions at the URL where I'm trying to save -- that is not the issue.  I've tried several variations of .Save(...), and nothing seems to work.  I'm sure it's something simple, but I can not figure it out.

     Thanks!

    Casey

    P.S. I am working off this tutorial: http://aspnet.4guysfromrolla.com/articles/012203-1.2.aspx
     

    Wednesday, June 25, 2008 1:57 AM

All replies

  • User494352855 posted

    I wouldn't use GetThumbnailImage() - use DrawImage instead. GetThumbnailImage() will return the pre-sized image thumbnail if available, which will look terrible.

    You can get the source code for my image resizer at http://nathanaeljones.com/products/asp-net-image-resizer/
     

     

     

    Thursday, August 28, 2008 9:25 AM
  • User967840515 posted
    Image.Save and Image.FromFile seem to both have massive bugs. You are better off doing it yourself.

    For this problem in Image.FromFile, use your own FileStream from File.OpenRead, and then use Image.FromStream.
    public static Image LoadImage(string fileName)
    {
        using (FileStream file = File.OpenRead(fileName))
            return Image.FromStream(file);
    }
    

    For this problem in Image.Save, create a new Bitmap with the correct height, width and pixel format then create a Graphics object from that, draw the original image onto it and save that one.
    public static void SaveImage(Image image, string fileName, ImageFormat format)
    {
        using (Bitmap savingImage = new Bitmap(image.Width, image.Height, image.PixelFormat))
        {
            using (Graphics g = Graphics.FromImage(savingImage))
                g.DrawImage(image, new Point(0, 0));
            savingImage.Save(fileName, format);
        }
    }
    

    I think that these bugs are both something to do with memory allocation in GDI+. They seem to take a while to occur but then once they start happening, the only way I can find to stop them (without changing the code to what I have said) seems to be to restart the machine.

    I've never had any problems with GetThumbnailImage itself and I assume that the solution in the other post only helps because it is not saving the original image.
    Saturday, August 30, 2008 11:37 AM