Friday, 22 September 2017

Asp.Net C# blank on Chart.SaveImage Saving chart from web give blank image

When export chart in c# using Chart1.SaveImage method , I got white image only.

I used below code to export chart as image , but I got as blank white screen only.

            MemoryStream stream = new MemoryStream();
            Chart1.SaveImage(stream, ChartImageFormat.Jpeg);
            BinaryReader binrayRdr = new BinaryReader(stream);
            byte[] bytes1 = ((MemoryStream)stream).ToArray();
            Response.Clear();
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", "attachment; filename=HTML.png");
            Response.Buffer = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes1);
            Response.End();


I found solution for this issue(may not be a right way) this may useful to someone who is struck on this.
I load the chart druing load_chart button click event
here ,
MemoryStream str = new MemoryStream();
Chart1.SaveImage(str);
Session["exportImage"] = str;
we have separate Export button , paste the following code in this event,
byte[] bytes1 = ((MemoryStream)Session["exportImage"]).ToArray();
            Response.Clear();
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", "attachment; filename=HTML.png");
            Response.Buffer = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes1);
            Response.End();

Its Working for me.

Tuesday, 19 September 2017

Asp.Net c# export Chart to Image

using System.IO;

protected void btnExport_Click(object sender, EventArgs e)
        {
            MemoryStream stream=new MemoryStream();           
            Chart1.SaveImage(stream, ChartImageFormat.Jpeg);
            BinaryReader binrayRdr = new BinaryReader(stream);
            byte[] bytes = ((MemoryStream)stream).ToArray();
            Response.Clear();
            Response.ContentType = "image/png";
            Response.AddHeader("Content-Disposition", "attachment; filename=HTML.png");
            Response.Buffer = true;
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.BinaryWrite(bytes);
            Response.End();
        }