This is a migrated thread and some comments may be shown as answers.
How to export chart image (serve as download)
5 Answers 265 Views
This is a migrated thread and some comments may be shown as answers.
Jimmy
Top achievements
Rank 1
Jimmy asked on 03 Sep 2009, 07:08 AM
Hello folks.

I was looking for a way to export my Telerik Chart image for use in documents and for printing, but didn't find a clean "Telerik solution" to the problem. The chart components features the Save(..) method, allowing us to save the generated image to the web-server. However, this is not what I wanted - I want to serve the image directly to the user as a download without writing it to the filesystem on the server.

Fortunately it is possible to save the image to a MemoryStream, so the following few lines does exactly what I need:

System.IO.MemoryStream ms = new System.IO.MemoryStream();  
this.chart.Save(ms, System.Drawing.Imaging.ImageFormat.Png);  
this.Page.Response.Clear();  
this.Page.Response.ClearHeaders();  
this.Page.Response.AddHeader("Content-disposition""attachment; filename=Chart.png");  
this.Page.Response.AddHeader("Content-type""image/png");  
this.Page.Response.BinaryWrite(ms.ToArray());  
this.Page.Response.End(); 

The browser will now pop up with the download dialog, allowing the user to either open or save the file.

If you want to have the image displayed in the browser, simply comment out the following line:
this.Page.Response.AddHeader("Content-disposition""attachment; filename=Chart.png");

Hopefully this tip will be useful to someone else :)

Telerik team: Wouldn't it be possible to implement the code above in an Export function, allowing less expirenced users to do export without worrying about memory streams, response headers and so on?

Best regards
Jimmy Thomsen

5 Answers, 1 is accepted

Sort by
0
Ves
Telerik team
answered on 07 Sep 2009, 10:00 AM
Hi Jimmy,

Thanks for sharing your solution. I have forwarded your implementation to our developers. Meanwhile, I am sure this forum post will prove helpful for those who need this functionality. I have updated your Telerik points.

Best Regards,
Ves
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Jimmy
Top achievements
Rank 1
answered on 08 Sep 2009, 12:12 PM
Hello Ves.

My pleasure. Thank you for passing it on to the dev. team and for the Telerik points :)

Best regards
Jimmy Thomsen
0
Cartoon Head
Top achievements
Rank 1
answered on 08 Mar 2012, 06:20 PM
That is awesome Jimmy!

For those who use VB.NET, here is the VB version:

Imports System.Drawing
Imports System.IO
  
Public Class YourForm
  
    Private Sub btnSave_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles btnSave.Click
  
        Dim ms As New System.IO.MemoryStream
  
        RadChart1.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
        Page.Response.Clear()
        Page.Response.ClearHeaders()
        Page.Response.AddHeader("Content-disposition", "attachment; filename=Chart.png")
        Page.Response.AddHeader("Content-type", "image/png")
        Page.Response.BinaryWrite(ms.ToArray())
        Page.Response.End()
  
    End Sub
  
End Class
0
Nagaraja
Top achievements
Rank 1
answered on 05 Sep 2012, 06:59 AM
Hi Jimmy,

This is very simple and clear. It was very helpful for my development. GOOD WORK!!!!!!
0
kishan
Top achievements
Rank 1
answered on 31 Mar 2014, 06:04 AM
hi
i dint get where this image save and this code is not working in my project
please help me
thanks in advance
Asked by
Jimmy
Top achievements
Rank 1
Answers by
Ves
Telerik team
Jimmy
Top achievements
Rank 1
Cartoon Head
Top achievements
Rank 1
Nagaraja
Top achievements
Rank 1
kishan
Top achievements
Rank 1
Share this question
or