Create a compressed image with higher resolution and fixed size

Hi,

I am trying to convert the named range into an image using the SheetRender API and setting the printArea on the pageSetup of the sheet that contains the data of the namedRange (Excel_Table) using the below mentioned code:

Workbook wb = new Workbook(“file.xlsx”);
Worksheet sheet = wb.getWorksheets().get(1); // The first sheet has the named range that I want converted to image.

PageSetup setup = sheet.getPageSetup();
setupCopiedSheet.setPrintArea(“A8:H50”); // the named range Excel_Table is contained within this range

ImageOrPrintOptions imgOrPrintOptions = new ImageOrPrintOptions();
imgOrPrintOptions.setImageFormat(ImageFormat.getPng());
imgOrPrintOptions.setOnePagePerSheet(true); // otherwise wider ranges don’t fit
imgOrPrintOptions.setHorizontalResolution(300);
imgOrPrintOptions.setVerticalResolution(300);

new SheetRender(sheet_copied, imgOrPrintOptions).toImage(0, byOut);
byOut.toByteArray(); // this array represents the image.

What I want to know is if there is a way to compress this image o that it takes less disk space without ruining the quality?

Also, when I do not set the resolution and use the default value of 96, the dimension of the image generated is smaller than what is generated using a resolution setting of 300. How do I fix the size of the image so that it is within the bounds I can specify but also generates an image of high resolution?

I tried using imageOrPrintOptions.setDesiredSize(int width, int height), but that impacts the final resolution, and the picture is not as clear as I would want it to be.

I have attached the following files with this post:

  1. ReferenceDoclet_withSingleCell.zip-- has the excel file that has the named range
  2. Excel_Table_fonts.png – the image generated with horizontal and vertical resolution set as 300. This image is too big (the width/height are too big)
  3. Excel_Table_fonts_withSizeSet.png – the image that i generated with resolution set as 300, but I also set desired size as the total width/height of the columns/rows contained within the named range in pixels. The problem that I have with this image is that it’s not as clear, the resolution does not seem to be 300 which is what I set it to.
  4. Excel_Table_fonts_default.png – the image generated using default resolution of 96 and without setting the desired size.

Hope you can provide some help on how I can regulate the resolution and the dimension of the image that I am generating.

Thanks

Excel_Table_fonts_default.png (11.1 KB)
Excel_Table_fonts_withSizeSet.png (10.8 KB)
Excel_Table_fonts.png (95.7 KB)
ReferenceDoclet_withSingleCell.zip (22 Bytes)

@oraspose

Thanks for using Aspose APIs.

The last zip file you attached is just 22 bytes and it is corrupt and cannot be opened. Please provide us correct Excel file so that we could test your issue at our end.

Please also download and try the latest version and check if it makes any difference and resolves your issue. Let us know your feedback.

Please note, if you are concerned about image quality and its dimensions, then save your image to EMF format.

Also see this article for your more help.

ReferenceDoclet_withImageInRange - Copy - Copy.zip (145.3 KB)

I have added the zip file. The dialogue box wants me to upload the file in zip format.

Thanks.

@oraspose

Thanks for using Aspose APIs.

We will look into your issue and help you asap.

@oraspose

For your needs, EMF image is the most good. When you place EMF image in Microsoft Excel or Microsoft Word, it does not get distorted, no matter, how much you zoom it out.

Other images like Png, Jpeg, Bmp gets distorted on zooming them out.

I have attached two files, please remove .zip from their names. One is EMF image and one is Excel file containing the EMF image.

out.emf.zip (487.2 KB)
EMF-Image-In-Excel.xlsx.zip (53.9 KB)

Here is the sample code to generate the EMF image.

C#

Workbook wb = new Workbook("ReferenceDoclet_withImageInRange - Copy - Copy.xlsx");

Worksheet ws = wb.Worksheets["Table Range"];

ImageOrPrintOptions opts = new ImageOrPrintOptions();
opts.ImageFormat = ImageFormat.Emf;
opts.OnePagePerSheet = true;

SheetRender sr = new SheetRender(ws, opts);
sr.ToImage(0, "out.emf");

If you want to render one-inch wide image, then if the resolution is 96, its width is 96 pixels, but if the resolution is 300, then its width is 300 pixels. So, on screen display, the 300 pixels one is wider than the 96 pixels.

Hi using emf is not an option for us.

What we want is an image with higher ppi, and also be able to fix the size of the image in inches so that it is not huge. So for example, I want my image to be 6 by 6 inches and with a 300 pixels per inch, and that the final image size in inches be calculated using 300 ppi, as opposed to the standard 96/75 dpi.

Thanks.

@oraspose

Thanks for your posting and considering Aspose APIs.

I have researched between DPI and PPI and found, PPI is not a good thing. Could you please provide some documents preferably simple documents that could explain the difference between them and explain why PPI is better than DPI. I found, one such document here, although it does not explain the difference between them clearly.

We generated an image 6 by 6 inches with a 300 pixels per inch, totally 1800 by 1800 pixels. Do you need images like this? If so, we will create a ticket for it so that we implement it in our future releases.

test_300DPI.zip (1.5 KB)