Chart to image conversion

6 Nov 20234 minutes to read

Refer to the following links for assemblies/nuget packages required based on platforms to convert the chart to image.

The following code snippet shows how to convert an Excel chart to an image using the ChartToImageConverter class.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;

  // Initialize XlsIORenderer
  application.XlsIORenderer = new XlsIORenderer();

  //Set converter chart image format to PNG
  application.XlsIORenderer.ChartRenderingOptions.ImageFormat = ExportImageFormat.Png;

  IWorkbook workbook = application.Workbooks.Open(File.OpenRead("Sample.xlsx"), ExcelOpenType.Automatic);
  IWorksheet worksheet = workbook.Worksheets[0];
  IChart chart = worksheet.Charts[0];

  //Creating the memory stream for chart image
  MemoryStream stream = new MemoryStream();

  //Saving the chart as image
  chart.SaveAsImage(stream);

  //Close and Dispose
  workbook.Close();
  stream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Xlsx;

  application.ChartToImageConverter = new ChartToImageConverter();
  application.ChartToImageConverter.ScalingMode = ScalingMode.Best;

  IWorkbook workbook = application.Workbooks.Open("Sample.xlsx");
  IWorksheet worksheet = workbook.Worksheets[0];
  IChart chart = worksheet.Charts[0];

  //Creating the memory stream for chart image
  MemoryStream stream = new MemoryStream();

  //Saving the chart as image
  chart.SaveAsImage(stream);
  Image image = Image.FromStream(stream);

  //Saving image stream to file
  image.Save("Output.png");
}
Using excelEngine As ExcelEngine = New ExcelEngine()
  Dim application As IApplication = excelEngine.Excel
  application.DefaultVersion = ExcelVersion.Xlsx

  Dim ChartToImageConverter As chartToImageConverter = New ChartToImageConverter()
  application.ChartToImageConverter = ChartToImageConverter
  application.ChartToImageConverter.ScalingMode = ScalingMode.Best

  Dim workbook As IWorkbook = application.Workbooks.Open("Sample.xlsx")
  Dim worksheet As IWorksheet = workbook.Worksheets(0)
  Dim chart As IChart = worksheet.Charts(0)

  'Creating the memory stream for chart image
  Dim stream As New MemoryStream()

  'Saving the chart as image
  chart.SaveAsImage(stream)
  Dim image As Image = Image.FromStream(stream)

  'Saving image stream to file
  image.Save("Output.png")
End Using

A complete working example to convert Excel chart to image in C# is present on this GitHub page.

NOTE

  1. Instance of XlsIORenderer class is mandatory to convert the chart to image using .NET Standard 2.0 assemblies.
  2. In .NET Standard, the Image format and quality can be specified using the ChartRenderingOptions property of XlsIORenderer class. By default the ImageFormat for chart is set to JPEG and ScalingMode is set to Best.
  3. Chart conversion to image and PDF are supported from .NET Framework 4.0 and .NET Standard 2.0 onwards
  4. Chart to Image conversion also works proper in both Blazor server-side and client-side.

Supported chart types

XlsIO supports the following chart types in image conversion.

Chart Type

Chart Subtypes

Area * Area
* Area_Stacked
* Area_Stacked_100
* Area_3D
Bar * Bar_Clustered
* Bar_Stacked
* Bar_Stacked_100
* Bar_Clustered_3D
* Bar_Stacked_3D
* Bar_Stacked_100_3D
Bubble Bubble
Column * Column_Clustered
* Column_Stacked
* Column_Stacked_100
* Column_3D
* Column_Clustered_3D
* Column_Stacked_3D
* Column_Stacked_100_3D
Doughnut * Doughnut
* Doughnut_Exploded
Line * Line
* Line_Markers
* Line_3D
Pie * Pie
* Pie_Exploded
* Pie_3D
* Pie_Exploded_3D
Radar * Radar
* Radar_Filled
* Radar_Markers
Scatter * Scatter_Line
* Scatter_Line_Markers
* Scatter_Markers
* Scatter_SmoothedLine
* Scatter_SmoothedLine_Markers
Stock * Stock_HighLowClose
* Stock_OpenHighLowClose
Excel 2016 Charts * Funnel
* Waterfall
* Histogram
* Pareto

NOTE

From the above supported chart types table, Waterfall and Line_3D charts are not supported in chart to image conversion in .NET Standard 2.0 onwards.

NOTE

Only embedded charts are supported in chart to image conversion. Chart sheets are not supported.

Supported chart elements

XlsIO supports the following chart elements in image conversion:
Chart to Image Conversion

Chart Elements:

  1. Axis
  2. Axis titles
  3. Chart title
  4. Data labels
  5. Grid lines
  6. Legend
  7. Trend line