This is a migrated thread and some comments may be shown as answers.

Printing a chart?

6 Answers 341 Views
ChartView
This is a migrated thread and some comments may be shown as answers.
Jesper
Top achievements
Rank 1
Jesper asked on 14 Aug 2012, 10:07 AM
Title says it.

How do I go about printing the chart at its current state (pan, zoom etc.)?
RadChart made it possible to export (save) the diagram as an image to a temp file that could be used but I don't see this feature with ChartView.

(Sorry if this question has been asked before but my attempts to search the forums came up with nothing.)

6 Answers, 1 is accepted

Sort by
0
Giuseppe
Telerik team
answered on 14 Aug 2012, 10:33 AM
Hello Jesper,

You can use the export-to-image extensions (part of Telerik.Windows.Controls.dll) to achieve the desired functionality for RadChartView like this (RadChart relies on the same extension methods but simply exposes them through its public API):
// RadChart1 is an instance of RadCartesianChart here
Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(RadChart1, memoryStream, new System.Windows.Media.Imaging.PngBitmapEncoder());

Hope this helps.


All the best,
Giuseppe
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Jesper
Top achievements
Rank 1
answered on 14 Aug 2012, 10:59 AM
I'm sure I've missed something trivial but I get an empty (white) image. This is the code I use to test:

private void printClick(object sender, RoutedEventArgs e)
{
    var chartImage = GetImage(chart);
    var w = new Window {Content = chartImage};
    w.Show();
}
 
public static UIElement GetImage(RadCartesianChart chart)
{
    using (var stream = new MemoryStream())
    {
        Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(chart, stream, new PngBitmapEncoder());
        stream.Seek(0, SeekOrigin.Begin);
        var decoder = new PngBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        var bitmapSource = decoder.Frames[0];
        var image = new Image {Source = bitmapSource};
        return image;
    }
}
0
Giuseppe
Telerik team
answered on 14 Aug 2012, 01:04 PM
Hello Jesper,

Here is a modified version of your GetImage(...) method that achieves the desired effect:
public static UIElement GetImage(RadCartesianChart chart)
{
    using (var stream = new MemoryStream())
    {
        Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(chart, stream, new PngBitmapEncoder());
 
        BitmapImage bitmapSource = new BitmapImage();
        bitmapSource.BeginInit();
        bitmapSource.CacheOption = BitmapCacheOption.OnLoad;
        bitmapSource.StreamSource = stream;
        bitmapSource.EndInit();
 
        var image = new Image { Source = bitmapSource };
 
        return image;
    }
}

Hope this helps.


All the best,
Giuseppe
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Jesper
Top achievements
Rank 1
answered on 14 Aug 2012, 03:17 PM
Unfortunately that also yields an empty image.

I wrote this extension method instead and now it all works fine.
Thanks anyway.

public static UIElement RenderImage(this FrameworkElement element)
{
    var bmp = new RenderTargetBitmap((int)element.ActualWidth, (int)element.ActualHeight, 96, 96, PixelFormats.Pbgra32);
    bmp.Render(element);
    var image = new Image { Source = bmp };
    return image;
}
0
Christian
Top achievements
Rank 1
answered on 29 Jul 2013, 10:37 AM
How can I extend this method to print the legend?
0
Ves
Telerik team
answered on 01 Aug 2013, 06:06 AM
Hi Christian,

Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage method accepts an argument of type FrameworkElement, so you can send a common parent of the chart and the legend as first parameter.

Best regards,
Ves
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
ChartView
Asked by
Jesper
Top achievements
Rank 1
Answers by
Giuseppe
Telerik team
Jesper
Top achievements
Rank 1
Christian
Top achievements
Rank 1
Ves
Telerik team
Share this question
or