ChartControl.ExportToImage(Stream, ImageFormat) Method
Creates an image file in the specified format from the current chart and exports it to a stream.
Namespace: DevExpress.XtraCharts
Assembly: DevExpress.XtraCharts.v20.1.UI.dll
NuGet Package: DevExpress.Win.Charts
Declaration
Parameters
Name | Type | Description |
---|---|---|
stream | Stream | A Stream object to which the current chart is exported. |
format | ImageFormat | A ImageFormat value representing the format in which the chart is exported. |
Remarks
Use this method to export the current chart as an image to an object of the Stream class or its descendants. The image format should be specified by the format parameter, for instance, ImageFormat.BMP. After creating an image file, the chart can be displayed in an appropriate application.
To show the standard Print dialog, use the ChartControl.Print method. The dialog allows end-users to print the chart, select the printer (if required), specify the range of pages to print, the number of copies, etc.
To display the DevExpress Print Preview use one of the following methods.
Method | Description |
---|---|
ChartControl.ShowPrintPreview | Creates the print document and displays the Print Preview of the document. |
ChartControl.ShowRibbonPrintPreview | Creates the print document and displays the Print Preview with the Ribbon toolbar of the document. |
To export the chart, use the appropriate ExportTo~ method (e.g., ChartControl.ExportToHtml, ChartControl.ExportToPdf, etc.)
Important
Note that, exporting to raster and vector images is implemented by the Chart and does not require any library.
The chart can be previewed, printed and exported to other formats only if the Printing library is available. Make sure you add a reference to the DevExpress.XtraPrinting.v20.1 assembly.
Also note that chart export to PDF requires the DevExpress.Pdf.Core library to be available.
Example
The following example demonstrates how to export a chart to an image. The GetChartImage method returns the image in the specified format, while the SaveChartImageToFile writes the chart’s image in the specified format to the specified path.
Create a ChartControl and populate it with data.
Note
Add references to the following assemblies to use the ChartControl in your application:
- DevExpress.Data.v20.1.dll
- DevExpress.Utils.v20.1.dll
- DevExpress.XtraEditors.v20.1.dll
- DevExpress.Charts.v20.1.Core.dll
- DevExpress.XtraCharts.v20.1.dll
- DevExpress.XtraCharts.v20.1.UI.dll
See Deployment for more information.
Use the ChartControl.ExportToImage method to create an image from a chart.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using DevExpress.XtraCharts;
namespace Series_PieChart {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
ChartControl pieChart;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
pieChart = new ChartControl();
Series series1 = new Series("Land Area by Country", ViewType.Pie);
series1.DataSource = DataPoint.GetDataPoints();
series1.ArgumentDataMember = "Argument";
series1.ValueDataMembers.AddRange(new string[] { "Value" });
pieChart.Series.Add(series1);
series1.Label.TextPattern = "{VP:p0} ({V:.##}M km²)";
series1.LegendTextPattern = "{A}";
pieChart.Dock = DockStyle.Fill;
this.Controls.Add(pieChart);
}
private void OnButtonClick(object sender, EventArgs e) {
SaveChartImageToFile(pieChart, ImageFormat.Png, "D://image1.png");
Image image = GetChartImage(pieChart, ImageFormat.Png);
image.Save("D://image2.png");
}
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);
}
}
public class DataPoint {
public string Argument { get; set; }
public double Value { get; set; }
public static List<DataPoint> GetDataPoints() {
return new List<DataPoint> {
new DataPoint { Argument = "Russia", Value = 17.0752},
new DataPoint { Argument = "Canada", Value = 9.98467},
new DataPoint { Argument = "USA", Value = 9.63142},
new DataPoint { Argument = "China", Value = 9.59696},
new DataPoint { Argument = "Brazil", Value = 8.511965},
new DataPoint { Argument = "Australia", Value = 7.68685},
new DataPoint { Argument = "India", Value = 3.28759},
new DataPoint { Argument = "Others", Value = 81.2}
};
}
}
}
Related GitHub Examples
The following code snippets (auto-collected from DevExpress Examples) contain references to the ExportToImage(Stream, ImageFormat) method.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.