Thursday, November 30, 2006

Code needed to dynamically write a .PNG from ASP.NET

For some strange reason you need to use the following code to dynamically output Jpeg's from an ASP.NET page.

Bitmap bitmap = new Bitmap (Path.Combine (dir , "link-notfound.png"));
bitmap.Save (Response.OutputStream, ImageFormat.Jpeg);

But a >PNG requires the following code :-

Bitmap bitmap = new Bitmap (Path.Combine (dir , "link-notfound.png"));
try
{
// This is the code required to send a PNG file. I'm not
// sure why it differs from the much simpler JPEG code
// bitmap.Save (Response.OutputStream, ImageFormat.Jpeg);
MemoryStream tempST = new MemoryStream();
bitmap.Save (tempST, ImageFormat.Png);
Response.ClearContent();
Response.ContentType = "image/png";
Response.BinaryWrite (tempST.ToArray());
Response.End();
}
catch
{
}
bitmap.Dispose();

1 comment:

Bruce said...

Today,we are proud to announce the launch of the new wedding support service sell ffxi gil,packed with features sure to sell ffxi gils delight adventurers across Vana'diel looking to exchange eternal vows with their beloved!Responding to player demands for greater customization,the new service will grant brides and grooms freedom in choosing location,timing,dialogue,and sell Final Fantasy XI Gil more for their ceremony,allowing them to create a truly memorable event all their own.Information on all the features,including in-game sell ffxi gil item vendors and wedding certificates,can be found on the new wedding support site,so head on over sell ffxi gils and get started planning the wedding of your dreams sell Final Fantasy XIGil!