Skip to content

Latest commit

 

History

History
67 lines (56 loc) · 1.92 KB

kb-chartview-export-to-image-in-code.md

File metadata and controls

67 lines (56 loc) · 1.92 KB
title page_title description type slug position tags ticketid res_type
Export RadChartView with SeriesProvider in Code
Save RadCartesianChart to Image Without Adding it to View
How to export RadChartView with series provider to picture without adding it to the visual tree.
how-to
kb-chartview-export-to-image-in-code
1417033
kb

Environment

Product Version 2019.2.618
Product RadChartView for WPF

Description

How to export RadChartView with a dynamic number of series (SeriesProvider) to a picture without adding it to the visual tree.

Solution

Set the Width and Height of the chart and then manually measure and arrange it. If the chart is hosted in a UserControl you will need to measure and arrange it as well.

Then use the ExportExtensions.ExportToImage method to export the chart.

[C#]

{{region kb-chartview-export-to-image-in-code-0}} private void ExportChartToImage() { var chartUserControl = new ChartUserControl(); // this UserControl is the view where the RadCartesianChart control is defined. PrepareElementForExport(chartUserControl);

	using (Stream fileStream = File.Open("../../myChartPicture.png", FileMode.OpenOrCreate))
	{
		Telerik.Windows.Media.Imaging.ExportExtensions.ExportToImage(chartUserControl, fileStream, new PngBitmapEncoder());
	}

}

private static void PrepareElementForExport(ChartControl userControl)
{
	var measureSize = new Size(500, 500);
	userControl.Measure(measureSize);
	userControl.Arrange(new Rect(measureSize));
	userControl.UpdateLayout();
 
	var chart = userControl.ChildrenOfType<RadCartesianChart>().FirstOrDefault();
	if (chart != null)
	{
		chart.Width = chart.RenderSize.Width;
		chart.Height = chart.RenderSize.Height;
		chart.OnApplyTemplate();
		chart.UpdateLayout();
	}
}

{{endregion}}