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?