Exporting in WPF Charts
Chart can be exported into image format. The following are the supported image formats:
- JPEG or JPG
- JPG-XR
- GIF
- PNG
- BMP
- TIFF
Export chart as an image
Chart provides the following overloaded methods for exporting it as an image.
The following methods used to export the chart as an image:
-
Save(string filename)- Exports the chart as an image to the specified location using the provided file name. -
Save(Stream, BitmapEncoder)- Exports the chart as an image to the specified stream using the provided bitmap encoder.
Save(string filename)
Use this method to export the chart to a specified location with a custom file name. If a location is not provided, the chart will be exported to the ../bin/Debug directory by default.
The following code examples illustrates the usage of this method:
private void SaveImage_Click(object sender, RoutedEventArgs e)
{
this.SampleChart.Save("ExportedChart.png"); //Save in Debug location
}We can change the image formats in above code by changing its extension as .jpg, .tiff, etc.
Save(Stream stream, BitmapEncoder imgEncoderID)
Use this method to export the chart to the specified stream using the provided bitmap encoder.
The following code examples illustrates the usage of this method:
private void SaveImageEncoder_Click(object sender, RoutedEventArgs e)
{
using (FileStream fileStream = new FileStream("ExportedChart.png", FileMode.Create))
{
SampleChart.Save(fileStream, new PngBitmapEncoder());
}
}The FileMode argument determines how the output file is created or accessed. The example uses FileMode.Create, which creates a new file or overwrites in an existing file. You can use other FileMode values such as CreateNew, Open, OpenOrCreate, Append, or Truncate depending on your file handling requirements.
Export without UI rendering
Export the chart as an image without rendering it in the UI by setting the chart as the RootVisual of an HwndSource and assigning HwndSourceParameters to the HwndSource. The following code example demonstrates this process.
static IntPtr ApplicationMessageFilter(IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam, ref bool handled)
{
return IntPtr.Zero;
}
HwndSourceParameters sourceParameters = new HwndSourceParameters();
sourceParameters.HwndSourceHook = ApplicationMessageFilter;
HwndSource source = new HwndSource(sourceParameters);
source.RootVisual = chart;
//Save chart
chart.Save("Chart.png");Export chart to XPS
Chart has built-in support for exporting to the XPS file format, providing scalable, print-ready output without any loss of quality when zooming or printing.
The following methods used to export the chart as an XPS file:
-
SaveAsXps(string fileName)- Exports the chart as an XPS file with the specified file name to the desired location. -
SaveAsXps(Stream stream)- Exports the chart as an XPS file using the specified stream.
SaveAsXps(string fileName)
Use this method to export the chart as a vector-based XPS file to a specified location with a custom file name. If a location is not provided, the chart will be exported to the ../bin/Debug directory by default.
The following code examples illustrates the usage of this method:
private void ExportAsXps_Click(object sender, RoutedEventArgs e)
{
SampleChart.SaveAsXps(Chart_Export.xps);
}SaveAsXps(Stream stream)
Use this method to export the chart as a vector-based XPS file using the specified stream.
The following code examples illustrates the usage of this method:
private void ExportAsStream_Click(object sender, RoutedEventArgs e)
{
using (FileStream fileStream = new FileStream("Chart_Export_Stream.xps", FileMode.Create))
{
SampleChart.SaveAsXps(fileStream);
}
}The FileMode argument determines how the output file is created or accessed. The example uses FileMode.Create, which creates a new file or overwrites in an existing file. You can use other FileMode values such as CreateNew, Open, OpenOrCreate, Append, or Truncate depending on your file handling requirements.
Export without UI rendering
Export the chart as an vector image without rendering it in the UI by setting the chart as the RootVisual of an HwndSource and assigning HwndSourceParameters to the HwndSource. The following code example demonstrates this process.
static IntPtr ApplicationMessageFilter(IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam, ref bool handled)
{
return IntPtr.Zero;
}
HwndSourceParameters sourceParameters = new HwndSourceParameters();
sourceParameters.HwndSourceHook = ApplicationMessageFilter;
HwndSource source = new HwndSource(sourceParameters);
source.RootVisual = chart;
//Save chart
chart.Save("Chart.xps");You can refer to our WPF Charts feature tour page for its groundbreaking feature representations. You can also explore our WPF Charts example to know various chart types and how to easily configure them with built-in support for creating stunning visual effects.