imclem/Highcharts-export-module-asp.net — Issue #6: A generic error occurred in GDI+ exporting png on IIS7.0
Author: GianlucaMaggio
Created: 2012-02-29 15:15:31+00:00
State: open

Exporting to png generate an error:

[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +378002
System.Drawing.Image.Save(Stream stream, ImageFormat format) +36
Tek4.Highcharts.Exporting.Exporter.WriteToStream(Stream outputStream) +234
Tek4.Highcharts.Exporting.Exporter.WriteToHttpResponse(HttpResponse httpResponse) +67
Tek4.Highcharts.Exporting.ExportChart.ProcessExportRequest(HttpContext context) +305
Tek4.Highcharts.Exporting.HttpHandler.ProcessRequest(HttpContext context) +7
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75

I found that the problem is known and a solution is to use a MemoryStream prior to write to response.
This is my code to solve the issue:

internal void WriteToHttpResponse(HttpResponse httpResponse)
{
httpResponse.ClearContent();
httpResponse.ClearHeaders();
httpResponse.ContentType = this.ContentType;
httpResponse.AddHeader("Content-Disposition", this.ContentDisposition);

if (this.ContentType == "image/png")
{
// for png we need to use a memory stream
using (MemoryStream ms = new MemoryStream())
{
WriteToStream(ms);
ms.WriteTo(httpResponse.OutputStream);
}
}
else
{
WriteToStream(httpResponse.OutputStream);
}