0

I've been searching for a resolution to this vexing problem and found quite a few answers, which unfortunately don't seem to work for me.

I have the following ASP.NET code that should output an image to the web browser as either JPEG or PNG:

public partial class MyImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    using(Bitmap bmp = new Bitmap(880, 520))
    {
        using (System.Drawing.Graphics gfx = System.Drawing.Graphics.FromImage(bmp))
        {
            try
            {
                gfx.Clear(Color.Aqua);
            //  finalizeOutput(bmp, gfx, MyImageType.SCGIT_JPEG);   //Works
                finalizeOutput(bmp, gfx, MyImageType.SCGIT_PNG);    //DOES NOT Work
            }
            catch (Exception ex)
            {
            }
        }
    }

}
    protected void finalizeOutput(System.Drawing.Graphics gfx, Bitmap bmp, MyImageType imageType)
    {
        //Get image type
        ImageFormat imgFmt;
        string strContentType;
        switch (imageType)
        {
            case MyImageType.SCGIT_GIF:
                strContentType = "image/gif";
                imgFmt = ImageFormat.Gif;
                break;
            case MyImageType.SCGIT_PNG:
                strContentType = "image/png";
                imgFmt = ImageFormat.Png;
                break;
            default:
                strContentType = "image/jpeg";
                imgFmt = ImageFormat.Jpeg;
                break;
        }

        //Finalizing and Cleaning Up 
        Response.ContentType = strContentType;
        bmp.Save(Response.OutputStream, imgFmt);    //THROWS EXCEPTION for png type: "A generic error occurred in GDI+."
//      bmp.Dispose();
//      gfx.Dispose();
        Response.End();
    }

}

What happens is that the above method works flawlessly from my VS2010 development IIS installed on Windows 7, but when I try the exact same code on Vista, it throws the "A generic error occurred in GDI+" exception, but only for the image type PNG, it works OK for JPEG.

Any idea how to solve this?

ahmd0
  • 16,633
  • 33
  • 137
  • 233
  • Where are those `bmp` and `gfx` variables initialized? IDisposable resources such as bitmaps and graphics should be as short lived as possible and always wrapped in using blocks to avoid leaking handles and ensure proper disposal. – Darin Dimitrov Jul 21 '12 at 17:44
  • They are initialized in the Page_Load when I'm prepping the image. After that my finalizeOutput is called to put it out to the browser. – ahmd0 Jul 21 '12 at 17:46
  • I would recommend you doing this in the same method and wrap the those IDisposable objects in using statements. – Darin Dimitrov Jul 21 '12 at 17:46
  • OK, I will. But how would that affect this exception? – ahmd0 Jul 21 '12 at 17:47
  • Failing to properly dispose those resources could result in this exception. – Darin Dimitrov Jul 21 '12 at 17:47
  • OK, let me recode it quickly and we'll see if it changes anything... hang on... – ahmd0 Jul 21 '12 at 17:48
  • OK. I just added 'using' blocks, rebooted this Vista system and, as I told you initially... the exception still comes up. – ahmd0 Jul 21 '12 at 18:03
  • I just updated it in my post. What else do you want to see? – ahmd0 Jul 21 '12 at 18:04
  • 1
    I want to see where you are instantiating those gfx and bmp variables. I didn't even notice you updated your post. I told you to use `using` statements. Not calling .Dispose manually. – Darin Dimitrov Jul 21 '12 at 18:05
  • OK, you're right. I didn't show it all. Just updated it to the exact way I call it. Note that disposing manually won't do any difference. – ahmd0 Jul 21 '12 at 18:18
  • So can anyone else help me find a resolution to this? – ahmd0 Jul 23 '12 at 22:41
  • Please read this: http://stackoverflow.com/questions/5629251/c-sharp-outputting-image-to-response-output-stream-giving-gdi-error – user2273820 Aug 12 '14 at 12:20

0 Answers0