When you try to save a Bitmap in a PNG format directly to Response.OutputStream like this:
Bitmap bmp = DayPilotMonth1.ExportBitmap(); bmp.Save(Response.OutputStream, ImageFormat.Png);
You may get the following error:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.
You can solve this problem by using a temporary MemoryStream:
// PNG requires random access to the output stream
using (MemoryStream mem = new MemoryStream())
{
bmp.Save(mem, ImageFormat.Png);
mem.WriteTo(Response.OutputStream);
}
Bitmap.Save() method requires random access to the output memory stream. This is not possible in case of a Response.OutputStream and an exception is thrown.