Printing a chart?

7 posts, 0 answers
  1. Jesper
    Jesper avatar
    18 posts
    Member since:
    Jun 2012

    Posted 14 Aug 2012 Link to this post

    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.)
  2. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 14 Aug 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
  4. Jesper
    Jesper avatar
    18 posts
    Member since:
    Jun 2012

    Posted 14 Aug 2012 Link to this post

    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;
        }
    }
  5. Giuseppe
    Admin
    Giuseppe avatar
    2363 posts

    Posted 14 Aug 2012 Link to this post

    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.

  6. Jesper
    Jesper avatar
    18 posts
    Member since:
    Jun 2012

    Posted 14 Aug 2012 Link to this post

    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;
    }
  7. Christian
    Christian avatar
    52 posts
    Member since:
    Apr 2011

    Posted 29 Jul 2013 Link to this post

    How can I extend this method to print the legend?
  8. Ves
    Admin
    Ves avatar
    2879 posts

    Posted 01 Aug 2013 Link to this post

    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 >>
Back to Top
UI for WPF is Visual Studio 2017 Ready