C# - Outputting image to response output stream giving GDI+ error
Solution 1:
PNGs (and other formats) need to be saved to a seekable stream. Using an intermediate MemoryStream
will do the trick:
using (Bitmap image = new Bitmap(context.Server.MapPath("images/stars_5.png")))
{
using(MemoryStream ms = new MemoryStream())
{
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
ms.WriteTo(context.Response.OutputStream);
}
}
Solution 2:
I just would add:
Response.ContentType = "image/png";
So it can be viewed directly in the browser when it isn't within an img
tag.