secret
Created

Outputting images in ASP.NET: The hard way

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
byte[] objImgData = ConvertImageToByteArray(new Bitmap(fileName), ImageFormat.Jpeg);
MemoryStream objMemoryStream = new MemoryStream();
objMemoryStream.Write(objImgData, 0, objImgData.Length);
Image objImage = Image.FromStream(objMemoryStream);
HttpContext.Current.Response.AddHeader("Content-Disposition", "filename=" + fileName);
HttpContext.Current.Response.ContentType = "image/Jpeg";
objImage.Save(HttpContext.Current.Response.OutputStream, ImageFormat.Jpeg);
...
 
private byte[] ConvertImageToByteArray(Image objImageToConvert, ImageFormat objFormatOfImage)
{
byte[] objByteImage;
 
using (MemoryStream objMemoryStream = new MemoryStream())
{
objImageToConvert.Save(objMemoryStream, objFormatOfImage);
objByteImage = objMemoryStream.ToArray();
}
 
return objByteImage;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.