We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Differences in Excel Image Rendering between ExcelInterop and Aspose

Hi Team,

I am facing issues with the rendering of excel into images in .emf format, there are visible differences in the images between excelInterop and Aspose images.

The issues are -

1.) The aspose image is shrunked vertically making the cell congested. It may be due to the reduction in row/cell height. Please how can this be fixed in the code.

2.) Size of aspose image is 469KBs comapared to the ExcelInterop image 29KB. How can we reduce the size of the image without losing image quality.

3.) How can we achieve the same image as generated by the excel library without any changes in the formattings.

4.) There also appears to be changes in the font after aspose image render. Please advise how thiscan be avoided.

Could you please advise these optimisations via the code snippets so it is better to understand.
Also please find the code snippet to replicate this behavior and the image attached for comparison purpose and to better understand the issue.

ImageRenderIssue.png (20.1 KB)

sample code to replicate the issue - RenderImage.zip (84.8 KB)

namespace AsposeLibUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            //License asposeLic = new License();
            //asposeLic.SetLicense("Aspose.Cells.NET.lic");

            FileStream fs;

            fs = File.Open("book1.xlsx", FileMode.Open);
            Workbook wb = new Workbook(fs);
            var sheet = wb.Worksheets["Rendering"];
            var range = sheet.Workbook.Worksheets.GetRangeByName("ImageRange");
            CreateImageMetafileFromRange(sheet,range);
        }

        private static void CreateImageMetafileFromRange(Worksheet sheet, Range range)
        {
            var path = Path.Combine(@"C:\Temp\" + Guid.NewGuid() + ".emf");            
            var pgSetup = sheet.PageSetup;
            pgSetup.PrintArea = range.Address;
            pgSetup.LeftMargin = 0.04;
            pgSetup.TopMargin = 0.04;
            pgSetup.RightMargin = 0.04;
            pgSetup.BottomMargin = 0.04;
            var options = new ImageOrPrintOptions();
            options.OnePagePerSheet = true;
            SheetRender sr = new SheetRender(sheet, options);
            sr.ToImage(0, path);
        }
    }

    public static class Extensions
    {
        public static void Apply<T>(this IEnumerable<T> source, Action<T> action)
        {
            foreach (var item in source)
            {
                action(item);
            }
        }
    }
}

Thanks & Regards
Shobhit.

@hi.shobhit82,
I have tested the sample code and the following are the observations.

This issue is reproduced.

I got the output file having the same size 469KBs as mentioned by you. Could you please share the other output file created by ExcelInterop for our reference?

We will try to generate the output file similar to the output created by ExcelInterop as much as possible.

I could not exactly identify this issue. Please elaborate.

Once you share the required information, we will log this issue for further investigation.

Please find the ExcelInterop Image - 29KB (unzip the below file to get the .emf file). In the source code provided in the original post I have also included an excelInterop project within the solution which can be run and image can be generated respectively.

a91532c1-6c83-4a5f-8f3a-dc17625061ce.zip (3.4 KB)

The font used in the excel appears to have changed after rendering to image by aspose, not sure if it is due to the change in the height of the cells. But it does not appear the same as used in the excel sheet. Please find a image below for comparison.

font_comparison_aspose_vs_excelInterop.png (1.7 KB)

You can use this source code provided in the zip which has corresponding aspose & excelInterop projects which has image generation logic from the excel sheet. Excels are also attached in the same project separately.

Thanks
Shobhit.

@hi.shobhit82,
Thank you for the feedback. We have observed the issue and logged it in our database for further investigation. You will be notified here once any update is ready for sharing.

This issue is logged as:
CELLSNET-48502 - Differences in Excel Image Rendering between ExcelInterop and Aspose

I missed to add one issue in this that when the image is finally rendered the font size of the text also appears to be bigger than the excel font size, the difference appears to be of one point. Could you please consider this while working out a solution. Thanks!

@hi.shobhit82,
Thank you for sharing this information. We have logged it with the ticket for our reference.

@hi.shobhit82,

Your ExcelInterop code range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlPicture); is copying Picutre “As shown on screen”. To get same result with Aspose.Cells, you should use ImageOrPrintOptions.OnlyArea = true instread of ImageOrPrintOptions.OnePagePerSheet = true which is used for copying Picutre “As shown when printed”.

Because ImageOrPrintOptions.ImageType = Aspose.Cells.Drawing.ImageType.Emf; is not set, the actual output image is BMP instead of Emf, after set ImageType to Emf, the file size of output image is 213KB. If the you still complaint about the file size, please let us know and we will create a separate ticket for it.

Because the font used in the source file is “Credit Suisse Type Light”, if the font is not installed, Aspose.Cells will do font substitutes. Please make sure the font “Credit Suisse Type Light” is installed or change to another font which is already installed.

After setting ImageType to Emf, I checked the font size in the Emf records of the output Emf image, the font size is same as the one in ExcelInterop generated Emf image.

In a word, the code for the your method CreateImageMetafileFromRange(Worksheet sheet, Range range) should be:

private static void CreateImageMetafileFromRange(Worksheet sheet, Range range)
{
    var path = Path.Combine(@"C:\Temp\" + Guid.NewGuid() + ".emf");            
    var pgSetup = sheet.PageSetup;
    pgSetup.PrintArea = range.Address;
    pgSetup.LeftMargin = 0.04;
    pgSetup.TopMargin = 0.04;
    pgSetup.RightMargin = 0.04;
    pgSetup.BottomMargin = 0.04;
    var options = new ImageOrPrintOptions();
    options.ImageType = Aspose.Cells.Drawing.ImageType.Emf;
    options.OnlyArea = true;
    SheetRender sr = new SheetRender(sheet, options);
    sr.ToImage(0, path);
}

Thanks for the response. I tried other samples to test the code which you suggested by using options.OnlyArea = true, but its behaving wierd in some cases where it cuts off the image, the option “OnePagePerSheet” works fine. Samples attached as below -

AsposeImage-525KB-OnePagePerSheet.emf
AsposeImage-455KB-OnlyArea.emf
ExcelInteropImage-88KB.emf
ImagesWithSize.zip (91.9 KB)

Size is still not acceptable as its giving us some issues when we are trying to store in the database. Could you please try to match up with the same size as we get with ExcelInterop libraries please.

As mentioned above size is still an issue.

The images which were shared earlier were generated from a system where the font was already installed (see screenshot from the system). I will try to replicate this with a different font which is available by default in excel and let you know if thats the issue.

Credit Suisse Fonts.PNG (16.5 KB)

Thanks
Shobhit.

@hi.shobhit82,
We have noted this information with the ticket for our reference and will share our feedback after detailed analysis.

@hi.shobhit82,

For your issue(s):

I tried other samples to test the code which you suggested by using options.OnlyArea = true, but its behaving wierd in some cases where it cuts off the image, the option “OnePagePerSheet” works fine.

It is because the sheet is split more than one pages, please set both OnlyArea and OnePagePerSheet.
e.g.
Sample code:

 var options = new ImageOrPrintOptions();
 options.ImageType = Aspose.Cells.Drawing.ImageType.Emf;
 options.OnlyArea = true;
 options.OnePagePerSheet = true;

Size is still not acceptable as its giving us some issues when we are trying to store in the database. Could you please try to match up with the same size as we get with ExcelInterop libraries please.

We will try to reduce the file size of the generated EMF image. We have logged a separate ticket “CELLSNET-48532” for it. We will update you here after evaluating it throughly.

The images which were shared earlier were generated from a system where the font was already installed (see screenshot from the system).

Please share the “Credit Suisse Type Light” font with us for testing (if possible).

Thanks for the reply, Yes, It works with setting both the options. I had tried that earlier, but however it does not bring much difference to the size.

I am afraid if that’s possible.

Good to know that this issue is sorted out by setting both attributes.

We need this font. So, kindly share the font with us. You may even upload to your file sharing service to give us download link to download the font file(s). We will evaluate your issue further.

Request you to please work on the remaining diff issues while i figure out a way to provide the fonts to you or replicate the issue with a standard font and let you know.

Please work on the image size issue and the vertical shrink issue as those are the most critical one’s to us.

Thanks
Shobhit

Regarding image size issue, we have already logged a separate ticket “CELLSNET-48532” for it. We will update once we figure it out or any new information is available. Regarding “the vertical shrink issue”, you already sorted by following our suggestion. You stated as:

:slight_smile: Sorry for the misunderstanding but by this I actually meant that it works as it was working earlier i.e. (the crop issue does not occur) but the issue of vertical shrink hasn’t shown any evidence that it has gone but I can do some more testing on that and confirm you.

@hi.shobhit82,
Please take your time to test it in detail and share your feedback along with images and sample files or any other supporting data.

[quote=“hi.shobhit82, post:8, topic:233306”]

As I had stated earlier please find attached the differences in the font and the image after rendering the excel range to an image. The difference also is visible when we use default fonts of the excel sheet, so the issue is not just limited to the Credit Suisse Fonts.

DiffWithFontCalibri.png (44.9 KB)
DiffWithFontArial.png (62.9 KB)

Source Code to replicate the issue - RenderImage-WithStandardFonts.zip (113.0 KB)

@hi.shobhit82,
We have logged the issue as “CELLSNET-48609” in our database for investigations. Once we will have some news for you, we will update you in this topic.

@hi.shobhit82,

We evaluated your issue (logged earlier as “CELLSNET-48609”) in details.

  1. For Image “Horizontally wider and Vertically shrunk” issue, as we had declared, please set ImageOrPrintOptions.OnlyArea to true. It will be OK.

  2. For Image quality issue, please directly save to EMF format Image(SheetRender.ToImage(0, path); ) instead of SheetRender->Tiff (a Raster image format)->Emf.
    So the final code will be like following:

string path = Path.Combine(@"C:\Temp\DEV\", Guid.NewGuid() + ".emf");            
string emfPath;
Worksheet worksheet = imageRange.Worksheet;
PageSetup pgSetUp = worksheet.PageSetup;
pgSetUp.PrintArea = imageRange.Address;

// Set all margins as 0
pgSetUp.LeftMargin = 0.04;
pgSetUp.RightMargin = 0.04;
pgSetUp.TopMargin = 0.04;
pgSetUp.BottomMargin = 0.04;

// Set OnePagePerSheet, OnlyArea option as true
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.ImageType = Aspose.Cells.Drawing.ImageType.Emf;            
options.OnePagePerSheet = true;
options.OnlyArea = true;

SheetRender sr = new SheetRender(worksheet, options);
sr.ToImage(0, path);

Hope, this helps a bit.

Have you tested this code ?

The reason I ask is because -
1.) “OnlyArea” property is not making any different to the width and height of the image.
2.) Image Type - EMF - we were using this only initially for a long time but we found that using .tiff image gives us a reduced size image.

Request you to please test the code you have pasted as it has already been tried and doesn’t work for us, but if you confirm this will work I can try again.

Thanks
Shobhit