Is custom resolution supported in worksheet to image conversion?
Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It also converts Excel documents to PDF files.
Currently, XlsIO doesn’t support converting a worksheet into an image with a custom resolution. However, we can achieve this by changing the resolution of an image before saving it. The following code snippet explains this:
C#
using (ExcelEngine excelEngine = new ExcelEngine()) { IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open("../../Data/Template.xlsx"); IWorksheet worksheet = workbook.Worksheets["Pivot Table"]; worksheet.UsedRangeIncludesFormatting = false; int lastRow = worksheet.UsedRange.LastRow + 1; int lastColumn = worksheet.UsedRange.LastColumn + 1; Image image = worksheet.ConvertToImage(1, 1, lastRow, lastColumn, ImageType.Bitmap, null); // Create a bitmap of specific width and height (500x500) Bitmap bitmap = new Bitmap((int)((image.Width * 500) / 96.0f), (int)((image.Height * 500) / 96.0f), PixelFormat.Format32bppPArgb); // Set the resolution (500). bitmap.SetResolution(500, 500); // Get graphics from bitmap with custom size. Graphics graphics = Graphics.FromImage(bitmap); // Recreate the image from stream using specified width and height. graphics.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height)); // Save the bitmap string fileName = "BitmapImg.png"; bitmap.Save(fileName, ImageFormat.Png); }
A complete working sample can be downloaded from CustomResolution.zip.
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to worksheets using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer this link to learn about generating and registering Syncfusion license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about if custom resolution supported in worksheet to image conversion in XlsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!