How to export chart image (serve as download)

Thread is closed for posting
6 posts, 0 answers
  1. Jimmy
    Jimmy avatar
    22 posts
    Member since:
    Nov 2008

    Posted 03 Sep 2009 Link to this post

    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
  2. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 07 Sep 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jimmy
    Jimmy avatar
    22 posts
    Member since:
    Nov 2008

    Posted 08 Sep 2009 Link to this post

    Hello Ves.

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

    Best regards
    Jimmy Thomsen
  5. Cartoon Head
    Cartoon Head avatar
    21 posts
    Member since:
    Apr 2010

    Posted 08 Mar 2012 Link to this post

    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
  6. Nagaraja
    Nagaraja avatar
    1 posts
    Member since:
    Sep 2012

    Posted 05 Sep 2012 Link to this post

    Hi Jimmy,

    This is very simple and clear. It was very helpful for my development. GOOD WORK!!!!!!
  7. kishan
    kishan avatar
    5 posts
    Member since:
    Mar 2014

    Posted 31 Mar 2014 Link to this post

    hi
    i dint get where this image save and this code is not working in my project
    please help me
    thanks in advance
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017