Problem with inserted images

Hi guys,

We’re using Aspose Cells 20.11 and when we insert an image, its width gets wider than it should.
This is the code we’re using:

public static void main(String[] args) throws Exception {
    final Workbook workbook = new Workbook();
    final Worksheet sheet = workbook.getWorksheets().get(0);
    final Cell cell = sheet.getCells().get(0, 0);

    final BufferedImage image = ImageIO.read(new File(BASE_PATH + "picture.png"));

    final int index = sheet
            .getPictures()
            .add(
                    cell.getRow(),
                    cell.getColumn(),
                    cell.getRow() + 1,
                    cell.getColumn() + 1,
                    encodeAsInputStream(image));

    final Picture pic = sheet.getPictures().get(index);

    addImage(image, pic, sheet, cell);
    workbook.save(BASE_PATH + "result.xlsx");
}

private static ByteArrayInputStream encodeAsInputStream(BufferedImage image) throws IOException {
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(image, "png", os);
    return new ByteArrayInputStream(os.toByteArray());
}

private static void addImage(BufferedImage image, Picture pic, Worksheet sheet, Cell cell) {
    pic.setLinkedCell(cell.getName());

    sheet.getCells().setRowHeightPixel(cell.getRow(), 277);
    sheet.getCells().setColumnWidthPixel(cell.getColumn(), 277);

    pic.setPlacement(PlacementType.MOVE);
}

Are we doing something wrong? We want the image to perfectly fit on the cell.

Best regards,
Hugo Freixo

@Hugo_Freixo,
The output I have produced with this code looks fine. Could you please share the input image, output file, expected output and screenshot for our analysis.

Archive.zip (26.4 KB)
The picture.png is a square and if you check our result.xlsx workbook you can see the image get stretched.

@Hugo_Freixo,
I have tried the scenario in a Windows 7 VM but could not observe any issue and image is square as expected. Could you please share your environment details for our reference.

ProperImage.png (44.6 KB)

We’re running this code on Mac OS. This is my result:
image.jpg (299.8 KB)
You can notice that on the image properties the width is 3.29’’ and the height is 2.88’’ although we set them both to 277px.

@Hugo_Freixo,
We will analyse this issue in Mac OS and share our feedback soon.

@hugo_freixo,

Run this code in Mac OS (or in MS Windows) and generate a file by setting height and width both to 277.

If we open this output file in MS Excel for MS Windows, you will see that its height and width is 277.
If we open same file in MS Excel for Mac OS, you will observe that its height is 207 and width is 237.

Well, this seems to be the difference in the behaviour of Excel for Windows 7 and Mac OS. You can see that in Windows, the ratio of height to width is the same as the original image. Images created in MS Windows and Mac OS are compared separately but in all cases, Excel for Windows has proper image size and Excel for macOS has a different ratio for height to width. We are afraid that this is not in our control and cannot be fixed.

Hi @ahsaniqbalsidiqui,

The thing is that this does not happen with previous version of aspose cells. We previously used Aspose Cells 19.2 and the problem did not occur.

@Hugo_Freixo,
I have executed the sample code using Aspose.Cells for Java 19.2 as well but no difference is observed. You may please share your output Excel file which is created in macOS using Aspose.Cells for Java 19.2. We will check this file and share our feedback.

Hi @ahsaniqbalsidiqui,

I also ran the same code with Aspose Cells 19.2 and you can clearly see that now the Height and Width are both set to 2.89’’.

Archive 2.zip (324.4 KB)

Best regards,
Hugo Freixo

@Hugo_Freixo,
I am afraid that I have opened this file side by side in Windows and macOS and you can see that both the Excel are displaying same file with different width. You can observe this scenario in the attached image. Hence this behavior is not linked with Aspose.Cells.
Comparison.jpg (402.3 KB)

@ahsaniqbalsidiqui,

I think you’re measuring the lengths of the cell where the image is.
I’m talking about the lengths of the actual image, although the cell should have the same size as the image.

@Hugo_Freixo,
Thank you for providing more information. This issue is observed and logged in our database for further investigation. You will be notified here once any update is ready for sharing.

This issue is logged as:
CELLSJAVA-43370 - Image size is different when added in macOS

Hi, can you tell me when will this problem be fixed?
Can you get us a beta version of the fix?

Best regards,
Hugo Freixo

@Hugo_Freixo,
This issue is not resolved yet. We will write back here once any update or ETA is available in this regard.

@Hugo_Freixo,
Please try the latest relase 20.12 with the following codes:

Workbook workbook = new Workbook(FileFormatType.XLSX);
ShapeCollection shapes = workbook.getWorksheets().get(0).getShapes();
shapes.addPicture(0, 0, 10, 10, new FileInputStream("D:\\Filetemp\\image1.png"));
shapes.get(0).setPlacement (PlacementType.MOVE);
shapes.get(0).setAnchorType( ShapeAnchorType.ONE_CELL_ANCHOR);
workbook.save("D:\\Filetemp\\dest.xlsx");

Let us know your feedback.