简体   繁体   中英

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

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 156X121.

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 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?

It's bizarre to me that the image is one size (126X151), its original size is purported to be 1.26" X 1.63", and its size after being scaled is 1.57" X 1.63".

Who or what is allowing this 25% increase in Height?

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?

UPDATE

Based on the answer, I tried this:

var ms = new MemoryStream();
//_logo.Height = 121; <= cannot set; Height is a readonly property
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);

Picture pic = pivotTableSheet.Pictures[0];     
//Workbook.Worksheets[0].Pictures[0]; <= does not compile
pic.HeightScale = 100;
pic.WidthScale = 100;

(Workbook.Worksheets[0] does not compile for me).

It makes no difference; the image is still stretching vertically.

UPDATE 2

I realized I needed "Workbook" to be "workBook" due to this:

private static Workbook workBook;

...and so I tried this:

Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable and subsequently gets hidden, so need to use 1
pic.Height = 121;
pic.WidthScale = 100;

...but it still gumbifies the image vertically. So does replacing "pic.Height = 121" with "pic.HeightScale = 100;"

So this is the code currently, which adds the image, but in a vertically gumbified manner:

var ms = new MemoryStream();
//_logo.Height = 121; readonly
_logo.Save(ms, ImageFormat.Png);
ms.Position = 0;
pivotTableSheet.Pictures.Add(0, _grandTotalsColumnPivotTable, ms);
Picture pic = workBook.Worksheets[1].Pictures[0]; // Worksheet 0 is the data sheet that feeds the PivotTable
//pic.Height = 121;
pic.HeightScale = 100;
pic.WidthScale = 100;

Please use this code to reset it to original height.

Picture pic = wb.Worksheets[0].Pictures[0];
pic.HeightScale = 100;
pic.WidthScale = 100;

Note: I am working as Developer Evangelist at Aspose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM