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

Screenshot of sparkline on server side

1 Answer 117 Views
Sparkline
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 16 Dec 2010, 11:37 AM
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.

1 Answer, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 21 Dec 2010, 01:52 PM
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);
}
 
sparkline.UpdateLayout();
Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(sparkline, pngOutput, new PngBitmapEncoder());
pngOutput.Flush();

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


Greetings,
Yavor Ivanov
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
Sparkline
Asked by
Alexander
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Share this question
or