windowsforms-2508-controls-and-libraries-chart-control-examples-producing-output-how-to-export-a-chart-to-image.md
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.
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}
};
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Windows.Forms
Imports DevExpress.XtraCharts
Namespace Series_PieChart
Partial Public Class Form1
Inherits DevExpress.XtraEditors.XtraForm
Private pieChart As ChartControl
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
pieChart = New ChartControl()
Dim series1 As 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
Me.Controls.Add(pieChart)
End Sub
Private Sub OnButtonClick(ByVal sender As Object, ByVal e As EventArgs) Handles simpleButton1.Click
SaveChartImageToFile(pieChart, ImageFormat.Png, "D://image1.png")
Dim image As Image = GetChartImage(pieChart, ImageFormat.Png)
image.Save("D://image2.png")
End Sub
Private Function GetChartImage(ByVal chart As ChartControl, ByVal format As ImageFormat) As Image
' Create an image.
Dim image As Image = Nothing
' Create an image of the chart.
Using s As New MemoryStream()
chart.ExportToImage(s, format)
image = System.Drawing.Image.FromStream(s)
End Using
' Return the image.
Return image
End Function
Private Sub SaveChartImageToFile(ByVal chart As ChartControl, ByVal format As ImageFormat, ByVal fileName As String)
' Create an image in the specified format from the chart
' and save it to the specified path.
chart.ExportToImage(fileName, format)
End Sub
End Class
Public Class DataPoint
Public Property Argument() As String
Public Property Value() As Double
Public Shared Function GetDataPoints() As List(Of DataPoint)
Return New List(Of DataPoint) From {
New DataPoint With {.Argument = "Russia", .Value = 17.0752},
New DataPoint With {.Argument = "Canada", .Value = 9.98467},
New DataPoint With {.Argument = "USA", .Value = 9.63142},
New DataPoint With {.Argument = "China",.Value = 9.59696},
New DataPoint With {.Argument = "Brazil",.Value = 8.511965},
New DataPoint With {.Argument = "Australia",.Value = 7.68685},
New DataPoint With {.Argument = "India",.Value = 3.28759},
New DataPoint With {.Argument = "Others",.Value = 81.2}
}
End Function
End Class
End Namespace