How to: Export a Chart to Image
- 2 minutes to read
The following example demonstrates how to use the ExportToImage method to create an image from a chart control. The first method returns the image in the specified format, while the second writes the chart’s image in the specified format to the specified path.
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using DevExpress.XtraCharts;
// ...
private Image GetChartImage(ChartControl chart, ImageFormat format) {
// Create an image.
Image image = null;
// Create an image of the chart.
using(MemoryStream s = new MemoryStream()) {
chart.ExportToImage(s, format);
image = Image.FromStream(s);
}
// Return the image.
return image;
}
private void SaveChartImageToFile(ChartControl chart, ImageFormat format, String fileName) {
// Create an image in the specified format from the chart
// and save it to the specified path.
chart.ExportToImage(fileName, format);
}