DayPilot Knowledge Base

AJAX Calendar/Scheduling Controls
DayPilot Pro (AJAX Calendar Control)
» DayPilot AJAX Calendar
DayPilot Pro (AJAX Monthly Calendar Control)
» DayPilot AJAX Monthly Calendar
DayPilot Pro (AJAX Scheduler Control)
» DayPilot AJAX Scheduler
DayPilot » Knowledge Base » Writing PNG to Response.OutputStream - System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Writing PNG to Response.OutputStream - System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

Last revision: May 22, 2012

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.

Related

How to add a header to the exported calendar image (PNG)
How to force download of exported calendar/scheduler image (PNG)
Sys.WebForms.PageRequestManagerParserErrorException on PNG export button click
How to save the exported PNG image to a file (without a web page)