How can I prevent my image from changing size when placing it on a spreadsheet?

I have an image that is embedded in my solution and is used on the main form of a Winforms app, and also for pasting into a spreadsheet. The image size is 156, 121

I put it on the sheet like so:

var ms = new MemoryStream();
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);

Yet when it is on the sheet, it stretches out and spills into neighboring cells, partially obscuring other data, as seen in the screenshot.

As you can see, the size is no longer 156X121. The height has been increased by 25%. Why? And how can I prevent that?

This code:

MessageBox.Show(string.Format("image height is {0}", _logo.Height));
MessageBox.Show(string.Format("image width is {0}", _logo.Width));

...showed me "126" as the height and "151" as the width, matching the image as it is in the project. So why is the original size changed? Is there a property I can set to leave the size alone and not stretch it? Or how can I prevent this gumbification of the image?

NOTE: If I select the "Reset" button in the image's "Size and Properties" dialog, it shrinks up as I want it to be, setting the Height "back" to 100% from 125%. Is there a way to do this "Reset" programmatically?

Hi Clay,


Thank you for contacting Aspose support.

In order to set the width & height of the inserted image, please get the Picture object from the collection and set Picture.Height & Width properties. Please note, you will find more similar properties which could get/set the dimensions in centimeters as well as inches. Please use appropriately.

In case the problem persists, please provide the source spreadsheet and the image so we could provide the exact solution for your requirements.

C#

<span style=“background-color: rgb(255, 255, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>var image = book.Worksheets[0].Pictures[0];
image.Height = //any value
image.Width = //any value

Hi again,


Adding more to my previous response, you can dynamically reset the Scale Height & Width of a picture while setting the Picture.WidthScale & HeightScale properties to 100. However, as per your sample spreadsheet (shared in another thread), we have noticed that the problem is caused due to the automatic row heights whereas stored row’s height (in the file) is not correct. You can avoid the situation by calling the Worksheet.AutoFitRows as demonstrated below or use it along with statements to reset the picture scales.

C#

var book = new Workbook(dir + “HILTON±+Produce+Usage±+from+Oct+2015_PROCESSED_0831.xlsx”);
var sheet = book.Worksheets[“PivotTableSheet”];
AutoFitterOptions options = new AutoFitterOptions();
options.OnlyAuto = true;
sheet.AutoFitRows(options);
//foreach (Picture picture in sheet.Pictures)
//{
// picture.Placement = Aspose.Cells.Drawing.PlacementType.FreeFloating;
// picture.WidthScale = 100;
// picture.HeightScale = 100;
//}
book.Save(dir + “output.xlsx”);

Perfect! I am suspending my hunger strike to have an apple danish.

Hi Clay,


Thank you for accepting the suggested solution. Please feel free to get in touch if you require any further assistance with Aspose APIs.