We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Is custom resolution supported in worksheet to image conversion?

Platform: WinForms |
Control: XlsIO |
Published Date: February 27, 2017 |
Last Revised Date: September 13, 2021

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

Currently XlsIO doesn’t have support to convert worksheet into image with 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 worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet 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.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile