Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

How to: Export a Chart to Image

  • 4 minutes to read

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.v24.2.dll
    • DevExpress.Utils.v24.2.dll
    • DevExpress.XtraEditors.v24.2.dll
    • DevExpress.Charts.v24.2.Core.dll
    • DevExpress.XtraCharts.v24.2.dll
    • DevExpress.XtraCharts.v24.2.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}
                };
        }
    }
}