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.2.UI.dll

Declaration

public void ExportToImage(
    Stream stream,
    ImageFormat format
)
Public Sub ExportToImage(
    stream As Stream,
    format As ImageFormat
)

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.2 assembly.

Also note that chart export to PDF requires the DevExpress.Pdf.Core library to be available.

Examples

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

See Also