When I insert an image using the following:
// cell and image are a Cell and a MemoryStream I pass into my method
var picIndex = worksheet.Pictures.Add(cell.Row, cell.Column, image)
the image will be stretched vertically. I can manually use the Excel property dialog to Reset and the image (originally a 128x128 Jpeg) will switch back to the desired appearance. I tried to simulate through code all those settings I see in Excel when the image looks as expected so I added a few more lines:
var pic = worksheet.Pictures[picIndex];
pic.IsLockAspectRatio = true;
pic.Placement = Aspose.Cells.Drawing.PlacementType.Move;
pic.RelativeToOriginalPictureSize = true;
pic.HeightScale = 100;
pic.HeightCM = pic.OriginalHeightCM;
pic.WidthScale = 100;
pic.WidthCM = pic.OriginalWidthCM;
but it doesn’t help. The image is still stretched vertically because when I have the file opened in Excel and I check the vertical scaling of the picture is somewhere over 900%.
Any ideas?
@mircea-ion
Thanks for using Aspose APIs.
Your code looks quite good. Please provide us your image so that we could test this issue at our end. We will look into it and help you asap.
@mircea-ion
Thanks for using Aspose APIs.
Please download and use the most recent version i.e.
It works fine. I have tested this issue with the following sample code. It adds the image using stream and file path and in both ways, added image is quite fine. Please see its output Excel file as well as screenshot.
Download Link:
Output Excel File.zip (10.0 KB)
C#
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
byte[] picBytes = File.ReadAllBytes("Unilever Spreads Business.jpg");
MemoryStream ms = new MemoryStream(picBytes);
ms.Position = 0;
//Add using stream
ws.Pictures.Add(2, 2, ms);
//Add using file path
ws.Pictures.Add(14, 2, "Unilever Spreads Business.jpg");
wb.Save("outputPictures.xlsx");
Screenshot
Yes, it works fine on a worksheet in a workbook that you just opened. Not in my case though.
Eventually I got to the point where I saved the workbook to disk after all my processing was done, excluding the image insertion, reopened the workbook inserted the image and it worked (I wish I started with that)
So, if you open the workbook and do some manipulations: smart markers processing, inserting rows, insert a page break … add an image, and while still in memory, if you save the workbook, the image will be stretched.
Our processing is relatively simple.
- start with a designer sheet where the first row has some smart markers
- process to populate the smart markers so we get some rows in the sheet.
- due to the content of some fields the result has some very tall rows so needs to be paginated (page break logic not relevant I guess)
- where the page breaks were established we insert two rows (title and header) and a page break above them.
- now as a test I tried inserting the image in cell A3 (where it should be located for the first content row)
- save workbook.
- check the file - image will probably be stretched
if before step 5 I save workbook, reopen and then insert image and save it works fine. it would be nice if it would work without having to save and reopen.
@mircea-ion
I am afraid, I am unable to replicate this issue. It seems, it might be the bug of some older version.
Please see the following code, its sample Excel and output Excel files, I changed the height of rows 2, 3 and 4 and nothing bad happened to image.
Please submit us the console application project that should be runnable and replicate your issue, we will execute it at our end and in case of some bug, log the issue for a fix.
Download Link:
Sample Input and Output Excel Files.zip (20.8 KB)
C#
Workbook wb = new Workbook("sample.xlsx");
Worksheet ws = wb.Worksheets[0];
Picture pic = ws.Pictures[0];
Cell cell = ws.Cells[pic.UpperLeftRow, pic.UpperLeftColumn];
ws.Cells.SetRowHeight(cell.Row-1, 40);
ws.Cells.SetRowHeight(cell.Row, 40);
ws.Cells.SetRowHeight(cell.Row+1, 40);
wb.Save("output.xlsx");