@@ -19,36 +19,30 @@ The following code snippet shows how to convert an Excel chart to an image using
1919{% highlight c# tabtitle="C# [ Cross-platform] " playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/Chart%20to%20Image/Chart%20to%20Image/.NET/Chart%20to%20Image/Chart%20to%20Image/Program.cs,180 " %}
2020using (ExcelEngine excelEngine = new ExcelEngine())
2121{
22- //Initialize application
23- IApplication application = excelEngine.Excel;
24-
25- //Set the default version as Xlsx
26- application.DefaultVersion = ExcelVersion.Xlsx;
22+ IApplication application = excelEngine.Excel;
23+ application.DefaultVersion = ExcelVersion.Xlsx;
2724
28- // Initialize XlsIORenderer
29- application.XlsIORenderer = new XlsIORenderer();
25+ // Initialize XlsIORenderer
26+ application.XlsIORenderer = new XlsIORenderer();
3027
31- //Set converter chart image format to PNG or JPEG
32- application.XlsIORenderer.ChartRenderingOptions.ImageFormat = ExportImageFormat.Png;
28+ //Set converter chart image format to PNG
29+ application.XlsIORenderer.ChartRenderingOptions.ImageFormat = ExportImageFormat.Png;
3330
34- //Set the chart image quality to best
35- application.XlsIORenderer.ChartRenderingOptions.ScalingMode = ScalingMode.Best;
31+ FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/InputTemplate.xlsx"), FileMode.Open, FileAccess.Read);
32+ IWorkbook workbook = application.Workbooks.Open(inputStream);
33+ IWorksheet worksheet = workbook.Worksheets[0];
3634
37- //Open existing workbook with chart
38- IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/InputTemplate.xlsx"));
39- IWorksheet worksheet = workbook.Worksheets[ 0] ;
40-
41- //Access the chart from the worksheet
42- IChart chart = worksheet.Charts[ 0] ;
35+ IChart chart = worksheet.Charts[0];
4336
44- #region Save
45- //Exporting the chart as image
46- FileStream outputStream = new FileStream(Path.GetFullPath("Output/Image.png"), FileMode.Create, FileAccess.Write);
47- chart.SaveAsImage(outputStream);
48- #endregion
37+ #region Save
38+ //Saving the workbook
39+ FileStream outputStream = new FileStream(Path.GetFullPath("Output/Image.png"), FileMode.Create, FileAccess.Write);
40+ chart.SaveAsImage(outputStream);
41+ #endregion
4942
50- //Dispose streams
51- outputStream.Dispose();
43+ //Dispose streams
44+ outputStream.Dispose();
45+ inputStream.Dispose();
5246}
5347{% endhighlight %}
5448
0 commit comments