How to Prevent Image Scaling When Inserting Images into Excel

Posted by: nazar.onishchenko on 18 October 2024, 7:55 am EST

  • Posted 18 October 2024, 7:55 am EST

    Hi, I’m wondering how to insert an image into Excel without any scaling.

    Currently, when I try to add an image to Excel (for example, an 800x800 SVG), and open the generated file, I notice that the image is scaled (‘Scale Width: 110%’). Why is this scaling happening, and how can I prevent it?

    Thanks!

    My code:

    public class InsertImageTest {
    
        @Test
        void insertImageToExcel() throws IOException {
            var workbook = new Workbook();
            var worksheet = workbook.getWorksheets().get(0);
            var shape = worksheet.getShapes().addPictureInPixel(
                Files.newInputStream(Path.of("image.svg")),
                ImageType.SVG,
                9.0,
                9.0,
                800,
                800
            );
            shape.setPlacement(Placement.FreeFloating);
    
            if (!Files.exists(Path.of("file.xlsx"))) {
                Files.createFile(Path.of("file.xlsx"));
            }
    
            workbook.save("file.xlsx", SaveFileFormat.Xlsx);
        }
    }

    Image: Any svg image from internet

    Grapecity Version: 7.2.3

  • Posted 21 October 2024, 9:10 am EST

    Hi,

    Apologies for the delay in response.

    We could not reproduce the issue you mentioned. Please refer to the attached GIF and sample project for reference. Please note that the image resolutions in the GIF are not exactly 800px because MS Excel shows the image resolutions in inches, centimeters, and millimeters rounded off to 2 decimal places.

    Could you please check our sample project and let us know if it reproduces the issue on your end? In case the issue persists, please share the following details:

    1. The image that shows the issue on your end
    2. Your system’s environment details
    3. Display scaling and layout

    Attachments: ImageScalingDemo.zip Excel Image Behavior.zip

    Best Regards,

    Kartik

  • Posted 22 October 2024, 6:06 am EST - Updated 22 October 2024, 6:15 am EST

    Hi Kartik,

    Thanks for your response.

    I tried your example, but I encountered the same scaling issue.

    I’ve attached my code sample and a screenshot to better illustrate the problem.

    excel-demo: excel-demo.zip

    Here are the details:

    1. I used the image from your sample.
    2. My setup: Java 21 (Eclipse Temurin), Apple M1 Pro running macOS Sonoma 14.5, and Microsoft Excel for Mac version 16.90.
    3. All other settings are at their defaults.

    Thanks again!

  • Posted 23 October 2024, 4:17 am EST

    Hi,

    We are able to replicate the change in behavior on our end, but it is not because of DsExcel. The behavior is how MsExcel handles rendering of images in different screen dimensions.

    When the file is saved on the MacBook’s File System and opened again in DsExcel Workbook, the dimensions of the image remain as it is, but if the file is opened in MsExcel instead of DsExcel, you will notice the change in dimensions of the image. You can further verify this behavior by changing the Display scaling of your MacBook and you will notice that the Image width scaling is now changed from 110% to a different value for the same file. The same behavior is there on other OS machines with different screen resolutions.

    MsExcel keeps the width and height of the image intact in pixels as they are and the screen resolution determines the pixel density of the screen, the image is scaled for different pixel densities. You can further contact the MsExcel team for this behavior.

    Refer to the attached sample that generates the required Xlsx file, opens the file again in DsExcel, prints the Shape’s dimensions (taken from the opened file) in pixels in the Console, and also opens the file in MsExcel for your reference.

    Attachment: scale.zip

    Please feel free to reach out if you encounter any other issues.

    Best Regards,

    Kartik

  • Posted 23 October 2024, 6:55 am EST

    Thank you, Kartik, for your investigation. It has been very useful to me, and I really appreciate it.

  • Posted 23 October 2024, 6:59 am EST - Updated 23 October 2024, 7:04 am EST

    Could you also share if it’s possible to control these checkboxes and scale values in code?

  • Posted 30 October 2024, 8:26 am EST

    Hi Kartik, sorry to bother you, but could you please take a look at my last message? It’s really important to me.

  • Posted 30 October 2024, 12:04 pm EST - Updated 30 October 2024, 12:20 pm EST

    Hi again,

    I found a really interesting point: the same image appears differently depending on the cell size settings.

    For example, try this code:

    Archive.zip

    In this code, I inserted the same image into two worksheets with different first-row/column settings, and the result is different scaling. For the first image, the dimensions are 100% width and 101% height, while for the second, they are 114% width and 101% height.

    It seems that the issue is not only related to display resolution but also to something else. I would be really grateful if you could check this behavior.

    It seems that when the first column is narrower than the image width, this redundant scaling occurs. However, when the column width is larger, there is no scaling at all.

    Thank you!

  • Posted 30 October 2024, 11:50 pm EST

    Hi,

    We are very sorry for the delay in response.

    The Scale Height and Scale Width of MsExcel are linked to the original Height and Width of the picture, therefore modifying these values will modify the height and width of the picture respectively. DsExcel exposes the methods setWidth, setHeight, setWidthInPixel, and setHeightInPixel methods in the IShape interface which can be utilized to scale the image. Refer to the below code snippet to scale height and width.

    private void scaleWidthInPixel(IShape shape, double scale, double initialWidth) {
        double currentWidth = 0;
        if(initialWidth == -1) {		  
            currentWidth = shape.getWidthInPixel();
        }
        else {
            currentWidth = initialWidth; 
        }
        double scaledWidth = currentWidth * scale / 100;
        shape.setWidthInPixel(scaledWidth);
    }
    private void scaleHeightInPixel(IShape shape, double scale, double initialHeight) {
        double currentHeight = 0;
        if(initialHeight == -1) {
        	currentHeight = shape.getHeightInPixel();
        }
        else {
        	currentHeight = initialHeight;
        }
        double scaledHeight = currentHeight * scale / 100;
        shape.setHeightInPixel(scaledHeight);
    }

    The Lock Aspect Ratio will help to scale the height and width together which again can be achieved through the width and height mutators of the IShape interface. Please refer to the code snippet below:

    private static void scaleDimensions(IShape shape, double scale, double initialWidth, double initialHeight) {
        scaleWidthInPixel(shape, scale, initialWidth);
        scaleHeightInPixel(shape, scale, initialHeight);
    }

    The Relative to original picture size manipulates the scale of the image concerning the original size of the image, when this checkbox is unchecked, the image scales according to the current dimensions rather than the initial dimensions, the arguments - initialWidth and initialHeight present in the above functions handles the same.

    You can further refer to the attached sample that implements the above code snippets and generates the scaled images.

    Please note that the scaling visible in MsExcel will be different for different screen resolutions as per the behavior of MsExcel with System Scaling and Layout settings. It can also be affected by other factors, which is internally controlled by MsExcel

    Attachment: scale_Updated.zip

    Best Regards,

    Kartik

  • Posted 31 October 2024, 4:15 am EST

    Thank you, Kartik, for your example; it’s very useful and has been a great help! When you have a moment, could you also take a look at my previous message with the attached archive? It shows a scenario where Excel applies different image scaling depending on the first column’s width. Specifically, when the first column is wider than the image, there’s no scaling; however, when it’s narrower, Excel scales the image width.

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels