We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Chart Export Service Image

Thread ID:





129672 Mar 28,2017 02:42 AM UTC Mar 28,2017 10:17 AM UTC ASP.NET MVC 1
Tags: Chart
Pedro Portelinha
Asked On March 28, 2017 02:42 AM UTC

I have to ask for help for the following, i need to create a service  that exports the Chart to in image.

It is possible to do this without having to make a post to a form page canvas?

 public void ExportChart()
            Chart _myChart = new Chart();
              .Series(sr =>
                  sr.Points(pt =>
              .Size(sz => sz.Height("450"))
              .Size(sz => sz.Width("700"))
              .Legend(lg => { lg.Visible(false); })
              .SeriesRendering("My chart")
               .PrimaryYAxis(px => px.Range(ra => ra.Min(0).Max(70).Interval(10)));
            MemoryStream stream = new MemoryStream(Convert.FromBase64String(_myChart));
            //using (Image image = Image.FromStream(stream))
            //    image.Save(@"ChartOutput.png", System.Drawing.Imaging.ImageFormat.Png);  
            //Disposing stream and images
            //Allowing client to download the saved image
            Context.Response.ContentType = "application/octet-stream";
            Context.Response.AddHeader("Content-Disposition", String.Format("attachment;filename=ChartExport.png"));

Thread Export Chart to Excel and PDF

This tread gives the example but is using javascript with canvas.

Thank for help.

Anandaraj T [Syncfusion]
Replied On March 28, 2017 10:17 AM UTC

Hi Pedro, 

Thanks for using Syncfusion products. 

We have prepared a simple sample based on your requirement and it can be downloaded from the following link 

In the above sample, we have used WebBrowser control to export the chart in server-side. Because, chart is rendered completely in client-side so it needs a browser for rendering. 

Please refer the following code snippet to achieve this 

         public void Export() 
             //Saving Chart as a PNG image using web browser control 
             System.Threading.Thread tr = new System.Threading.Thread(() => ExportChart()); 
             string fileName = Server.MapPath(@"\ChartExport.png"); 
             //Make current thread wait until chart image is ready in STA thread 
             //Read chart image as bytes 
             byte[] file = System.IO.File.ReadAllBytes(fileName); 
             //Delete the file after reading 
             //Allowing client to download the image 
             Response.OutputStream.Write(file, 0, file.Length); 
             Response.ContentType = "application/octet-stream";              
             Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); 
         public void ExportChart() 
             //Set properties of chart in server-side 
             Chart _myChart = new Chart(); 
             var prop = _myChart.Chart("Text") 
               .Series(sr => 
                   sr.Points(pt => 
               .Size(sz => sz.Height("450")) 
               .Size(sz => sz.Width("700")) 
               .Legend(lg => { lg.Visible(false); })                             
               .PrimaryYAxis(px => px.Range(ra => ra.Min(0).Max(70).Interval(10)));         
             //Create web browser control 
             WebBrowser browser = new WebBrowser() { ScriptErrorsSuppressed=true, WebBrowserShortcutsEnabled = false, Size = new System.Drawing.Size(1000, 1000) }; 
             //Serialize chart properties as a string 
             string chartOption = _myChart.Serialize(prop.chart.ChartModel); 
             browser.DocumentText = new StringBuilder() 
                 //Add header for HTML document 
                 .Append("<!DOCTYPE html><html><head><meta http-equiv='X-UA-Compatible' content='IE=edge' />" 
                 //Add scripts required for rendering Chart in browser control 
                 + AddScripts(browser, chartOption) 
                 + "</head><body><div id='container'></div></body></html>").ToString(); 
             //Check browser is loaded or not. If it is not ready wait until browser loading is complete 
             //Since this is happening in a separate thread it will not affect load time of web page 
             while (browser.ReadyState == WebBrowserReadyState.Loading) 
             //Get the bounds of chart in browser control 
             var bounds = browser.Document.InvokeScript("createChart").ToString().Split('-'); 
             //Save chart as an image in server 
             using (Bitmap img = new Bitmap(ParseInt(bounds[1]), ParseInt(bounds[0]))) 
                 browser.DrawToBitmap(img, new Rectangle(ParseInt(bounds[2]), ParseInt(bounds[3]), img.Width, img.Height)); 
                 img.Save(Server.MapPath(@"\ChartExport.png"), ImageFormat.Png); 

Please let us know if you have any concern. 



This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon