Skip to main content
A newer version of this page is available.

ChartControl.ExportToImage(String, ImageFormat) Method

Creates an image file in the specified format from the current chart and outputs it to the specified path.

Namespace: DevExpress.XtraCharts

Assembly: DevExpress.XtraCharts.v19.1.UI.dll

Declaration

public void ExportToImage(
    string filePath,
    ImageFormat format
)

Parameters

Name Type Description
filePath String

A String containing the full path to where the image file will be created.

format ImageFormat

A ImageFormat value representing the format in which the chart is exported.

Remarks

If a file under the specified file path exists, the new file replaces it.

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

See Also