|
| 1 | +--- |
| 2 | +title: Exporting Charts from Excel Documents to PNG Images with a Specific DPI Resolution Using SpreadProcessing and PdfProcessing |
| 3 | +description: Learn how to export Excel charts to PNG images with a Specific DPI resolution using Telerik SpreadProcessing, PdfProcessing and UI for WinForms and WPF charting controls. |
| 4 | +type: how-to |
| 5 | +page_title: Exporting Charts from Excel Documents to PNG Images with a Specific DPI Resolution Using SpreadProcessing |
| 6 | +meta_title: Exporting Charts from Excel Documents to PNG Images with a Specific DPI Resolution Using SpreadProcessing |
| 7 | +slug: export-charts-png-300dpi-spreadprocessing |
| 8 | +tags: spread, processing,telerik, document ,chart, image, export, png, dpi, resolution |
| 9 | +res_type: kb |
| 10 | +ticketid: 1695547 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.2.520| RadSpreadProcessing & RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +Learn how to extract the [charts]({%slug radspreadprocessing-features-charts%}) from Excel documents and save them as PNG files, while specifying the desired resolution (e.g. 300 DPI). |
| 22 | + |
| 23 | +## Solution |
| 24 | + |
| 25 | +Exporting charts directly from Excel files to images is not yet supported by the [SpreadProcessing]({%slug radspreadprocessing-overview%}) library. As an alternative, you can benefit the export option to PDF format which allows you to plug into the chart rendering process, export the chart and handle the DPI settings |
| 26 | + |
| 27 | +### Suggested Workflow Using Charting Controls |
| 28 | + |
| 29 | +1. Extract the chart data from the Excel file using SpreadProcessing. |
| 30 | +2. Rebuild the chart using a UI component, such as [RadChartView](https://docs.telerik.com/devtools/winforms/controls/chartview/features/export) from the Telerik UI for WinForms or WPF suites. |
| 31 | +3. Export the chart as a PNG image with 300 DPI using RadChartView. |
| 32 | + |
| 33 | +### Alternative Approach: Exporting to PDF |
| 34 | + |
| 35 | +Export the XLSX document to PDF format using SpreadProcessing. This method internally uses a **chart renderer** for rendering charts in the PDF. Implement a custom [IPdfChartRenderer]({%slug radspreadprocessing-features-charts-pdf-export%}) to manipulate chart resolution and save the chart as a PNG image in the ongoing export process. |
| 36 | + |
| 37 | +#### Sample implementation for exporting charts as PNG with 300 DPI: |
| 38 | + |
| 39 | +```csharp |
| 40 | +public class WinFormsPdfChartImageRenderer : IPdfChartRenderer |
| 41 | +{ |
| 42 | + public void RenderChart(FixedContentEditor editor, FloatingChartShape chartShape) |
| 43 | + { |
| 44 | + string filePath = @"exportedChart.png"; |
| 45 | + |
| 46 | + // Set chart dimensions |
| 47 | + System.Drawing.Size size = new System.Drawing.Size((int)(chartShape.Width), (int)(chartShape.Height + 10)); |
| 48 | + System.Drawing.Image chartImage = Telerik.WinForms.Controls.Spreadsheet.Layers.ChartModelToImageConverter.GetImageFromFloatingChartShape(chartShape, size); |
| 49 | + |
| 50 | + using (var bmp = new Bitmap(chartImage)) |
| 51 | + { |
| 52 | + // Set DPI to 300 |
| 53 | + bmp.SetResolution(300, 300); |
| 54 | + |
| 55 | + // Save as PNG |
| 56 | + bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); |
| 57 | + } |
| 58 | + using (FileStream fs = new FileStream(filePath, FileMode.Open)) |
| 59 | + { |
| 60 | + editor.DrawImage(fs); |
| 61 | + } |
| 62 | + Process.Start(new ProcessStartInfo(filePath) { UseShellExecute = true }); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +To export charts to PNG, use the custom renderer in conjunction with SpreadProcessing’s PDF export functionalities. Charts saved as PNG images can then be adjusted for resolution. |
| 68 | + |
| 69 | +#### Applying the custom renderer: |
| 70 | + |
| 71 | +```csharp |
| 72 | +Telerik.Windows.Documents.Spreadsheet.Model.Workbook workbook; |
| 73 | +IWorkbookFormatProvider formatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml.Xlsx.XlsxFormatProvider(); |
| 74 | +string fileName = "SampleFile.xlsx"; |
| 75 | +using (Stream inputStream = new FileStream(fileName, FileMode.Open)) |
| 76 | +{ |
| 77 | + workbook = formatProvider.Import(inputStream, TimeSpan.FromSeconds(10)); |
| 78 | +} |
| 79 | + |
| 80 | +PdfFormatProvider pdfFormatProvider = new Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf.PdfFormatProvider(); |
| 81 | +pdfFormatProvider.ExportSettings.ChartRenderer = new WinFormsPdfChartImageRenderer(); |
| 82 | +converter.PdfFormatProvider = pdfFormatProvider; |
| 83 | +using (Stream output = File.OpenWrite("Sample.pdf")) |
| 84 | +{ |
| 85 | + pdfFormatProvider.Export(workbook, output, TimeSpan.FromSeconds(10)); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Note: Recreating the chart using RadChartView may require extra effort if the charts are highly customized or complex. |
| 90 | + |
| 91 | +## See Also |
| 92 | + |
| 93 | +- [SpreadProcessing PDF Export]({%slug radspreadprocessing-features-charts-pdf-export%}) |
| 94 | +- [Exporting Spreadsheets with Charts to PDF with RadSpreadProcessing and WinForms RadChartView]({%slug export-charts-to-pdf-radspreadprocessing%}) |
0 commit comments