Silverlight: RadChart for Export to Image

Tuesday, September 08, 2009 by Vladimir Milev | Comments 4

Good news everyone! Exporting Silverlight charts to images has been a common request for our Silverlight chart. I’m glad to announce that the functionality is already done and checked in our development branch. This means that everything is on track for the Q3 2009 release (and maybe a beta release before that!).

 

But why not for Q2? Well, there are a couple of reasons. Q2 release was still Silverlight 2 based and there were technological difficulties that didn’t allow us to implement this feature. Firstly, Silverlight 2 didn’t have the writeable bitmap class which facilitates rasterizing the chart rendering and secondly there was no way to write to a file. Now that Silverlight 3 is out there are no roadblocks and we’ve implemented the export functionality. I will walk you through it in the next paragraph.

 

Consider the following code snippet:

//PART 1 - Show the dialog and obtain a stream to the new file
SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "png";
dialog.Filter = "*.png | PNG File";

if (!(bool)dialog.ShowDialog())
return;

Stream fileStream = dialog.OpenFile();

//PART 2 - use the stream to write the file output.
RadChart1.ExportToImage(fileStream, new Telerik.Windows.Media.Imaging.PngBitmapEncoder());
fileStream.Close();

As you can see the code is split into two parts. The first part should be familiar to all silverlight 3 develoeprs. It uses the new SaveFileDialog class to display a save dialog and obtain a reference to a stream to the newly created file. The second part demonstrates how to pass that stream to the RadChart API and export the chart content to a PNG file. Since Silverlight does not have any encoders for PNG or BMP you will need to use the encoders we provided to you along RadChart. These are our own encoders which are implemented according the PNG and BMP specifications. The BMP encoder name is Telerik.Windows.Media.Imaging.BmpBitmapEncoder.

 

But there’s more! Not only did we implement exporting to PNG and BMP, but there is a way to export to EXCEL and XPS too. To do so you simply need to use a different method:

RadChart1.ExportToExcelML(fileStream);
RadChart1.ExportToXps(fileStream);

There are a lot of other things the charting team has been busy with. Stay tuned to the telerik blogs to learn about more exciting features to come out for Q3 2009.

4 Comments

  • Ruddy Santos 09 Sep 2009
    Is that functionality going to be in the non-silverlight chart control?

    Ruddy

  • Vladimir Milev 11 Sep 2009
    Yes, this is available for ASP.NET and Winforms controls as well as a Save() method.
    Also, this same functionality is available in WPF as well.
  • Marc 12 Oct 2009
    Hey Vladimir,

      I was wondering if this function can be used to feed a WritableBitmap ?

    Here is a sample of what I'm trying to do :
    ...
                Stream imageStream = new MemoryStream();
                currentChart.ExportToImage(imageStream, new BmpBitmapEncoder() { });
                WriteableBitmap w = new WriteableBitmap(0, 0);
                w.SetSource(imageStream); // This fails

    I encounter this critical failure :
    Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))

    Any solution ? I tried with the PngBitmapEncoder() with the same error. I guess the Png/Bmp codec is not compatible with WritableBitmap.

    Thanks,
    Marc.
  • Sireesha 13 Jul 2010

    I am trying to export my WPF chart control to an image but I am getting an exception saying "Object reference not set". I am wondering what I am doing wrong here.
    I am trying to export to xps. 
    My another requirement is to convert to jpeg.
    Can you guys please help me doing this?


    SeriesMapping

     

     

    sm1 = new SeriesMapping();
    sm1.SeriesDefinition =
    new LineSeriesDefinition();
    sm1.LegendLabel =
    "Line Series 1";
    sm1.CollectionIndex = 0;
    ItemMapping im1 = new ItemMapping();
    im1.DataPointMember =
    DataPointMember.YValue;
    sm1.ItemMappings.Add(im1);
    SeriesMapping sm2 = new SeriesMapping();
    sm2.SeriesDefinition =
    new LineSeriesDefinition();
    sm2.LegendLabel =
    "Line Series 2";
    sm2.CollectionIndex = 1;
    ItemMapping im2 = new ItemMapping();
    im2.DataPointMember =
    DataPointMember.YValue;
    sm2.ItemMappings.Add(im2);
    var itemsSource = new List<double>[] { new List<double> { 9, 2, 3, 4 }, new List<double> { 5, 7, 3, 4 } };
    this.RadChart1.SeriesMappings.Add(sm1);
    this.RadChart1.SeriesMappings.Add(sm2);
    this.RadChart1.ItemsSource = itemsSource;
    Microsoft.Win32.
    SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
    dlg.DefaultExt =
    ".xps";
    string fileName = string.Empty;

     

     

     

    if (!((bool)dlg.ShowDialog()))
    {
    fileName = dlg.FileName;
    }

     

     

     

    this.RadChart1.ExportToXps(fileName);

     

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)