Search code examples
c#asp.netimagechartsmemorystream

Export file using image saved to memory stream


I am wanting to save an image to file which has been stored in a memory stream.

I have saved a asp.net chart to a memory stream.

stream1 = new MemoryStream();
chart_location_3.SaveImage(stream1, ChartImageFormat.Png);

and i am then using the following code to export to a jpg. It triggers the save to prompt and creates the file but the image will not open ("this is not a valid bitmap file, or its format is not currently supported")

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

System.Drawing.Image img;

img = System.Drawing.Image.FromStream(stream1);
response.ClearContent();
response.Clear();
Response.ContentType = "image/jpg";
response.AddHeader("Content-Disposition", "attachment; filename= Exported.jpg;");
response.Write(img);
response.Flush();
response.End();

Solution

  • changed the response write to:

    response.BinaryWrite(stream1.ToArray());