Chart to image conversion

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 ExcelChartToImageConverter class.

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

  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.Excel2013

  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
//Chart To Image conversion can be performed by referring .NET Standard assemblies in UWP platform.

using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;
  
  //Initializing XlsIORenderer
  application.XlsIORenderer = new XlsIORenderer();

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

  //Gets assembly
  Assembly assembly = typeof(App).GetTypeInfo().Assembly;

  //Gets input Excel document from an embedded resource collection
  Stream inputStream = assembly.GetManifestResourceStream("Sample.xlsx");

  IWorkbook workbook = application.Workbooks.Open(inputStream, ExcelOpenType.Automatic);
  IWorksheet sheet = workbook.Worksheets[0];

  IChart chart = worksheet.Charts[0];  

  //Initializes FileSavePicker
  FileSavePicker savePicker = new FileSavePicker();
  savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
  savePicker.SuggestedFileName = "Output";
  savePicker.FileTypeChoices.Add("Image Files", new List<string>() { ".png",".jpeg",".jpg" });

  //Creates a storage file from the FileSavePicker
  StorageFile storageFile = await savePicker.PickSaveFileAsync();

  //Converts and save to stream
  var file = await storageFile.OpenAsync(FileAccessMode.ReadWrite);
  Stream stream = file.AsStreamForWrite();
  chart.SaveAsImage(stream);
  await file.FlushAsync();
  stream.Dispose();
}
using (ExcelEngine excelEngine = new ExcelEngine())
{
  IApplication application = excelEngine.Excel;
  application.DefaultVersion = ExcelVersion.Excel2013;

  // 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;

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

  //Set converter chart image format to PNG
  application.XlsIORenderer.ChartRenderingOptions.ImageFormat = ExportImageFormat.Png;
  
  //Gets assembly
  Assembly assembly = typeof(App).GetTypeInfo().Assembly;

  //Gets input Excel document from an embedded resource collection
  Stream inputStream = assembly.GetManifestResourceStream("Sample.xlsx");

  IWorkbook workbook = application.Workbooks.Open(inputStream);
  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);

  stream.Position = 0;

  //Save the document as file and view the saved document

  //The operation in SaveAndView under Xamarin varies among Windows Phone, Android, and iOS platforms. Refer to the xlsio/xamarin section for respective code samples.
  if (Device.OS == TargetPlatform.WinPhone || Device.OS == TargetPlatform.Windows)
      DependencyService.Get<ISaveWindowsPhone>()
          .SaveAndView("Test.png", "image/png", stream);
  else
      DependencyService.Get<ISave>().SaveAndView("Test.png", "image/png", stream);
}

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 Blazor server-side alone and not in 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.

Supported chart elements

XlsIO supports the following chart elements in image conversion:
chart elements

Chart Elements:

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