Screenshot of sparkline on server side

2 posts, 0 answers
  1. Alexander
    Alexander avatar
    18 posts
    Member since:
    Jul 2010

    Posted 16 Dec 2010 Link to this post

    Hi, how can I get screenshot of sparkline?

    I do the following:

    RadSparklineBase sparkline = new RadLinearSparkline();sparkline.XValuePath = "X";sparkline.YValuePath = "Y";sparkline.Margin = new Thickness(0);sparkline.Width = width;sparkline.Height = height;sparkline.ItemsSource = new SparkPoint[]{new SparkPoint(10, 10),new SparkPoint(10, 20),new SparkPoint(20, 10),new SparkPoint(10, 5)};DrawingVisual visual = new DrawingVisual();using (visual.RenderOpen()){new VisualBrush sparkline);}using (MemoryStream stream = new MemoryStream()){Rect rect = new Rect(0, 0, (int)width, (int)height);RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32);sparkline.Arrange(rect);renderTargetBitmap.Render(sparkline);PngBitmapEncoder pngBitmapEncoder = new PngBitmapEncoder();pngBitmapEncoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));pngBitmapEncoder.Save(stream);stream.Flush();image = Image.FromStream(stream);}

    Is it possible to make it work? Thanks.
  2. Yavor
    Yavor avatar
    401 posts

    Posted 21 Dec 2010 Link to this post

    Hello Alexander,

    It is possible to render a sparkline without adding it to the Visual Tree. In the Telerik.Windows.Controls assembly are defined several helper methods that can help you save any visual element like this:

    Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(sparkline, pngOutput, new PngBitmapEncoder());

    Using that method you can save a sparkline in memory without adding it to the visual tree (window) like this:

    sparkline.Measure(new Size(width, height));
    sparkline.Arrange(new Rect(sparkline.DesiredSize));
    DrawingVisual visual = new DrawingVisual();
    using (visual.RenderOpen())
        new VisualBrush(sparkline);
    Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(sparkline, pngOutput, new PngBitmapEncoder());

    I have modified your sample code to use the approach mentioned above and attached it to that post.
    Hope this helps!

    Yavor Ivanov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
Back to Top