[英]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. 我有一个图像嵌入到我的解决方案中,并用于Winforms应用程序的主要形式,也用于粘贴到电子表格中。 The image size is 156X121.
图像尺寸为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. 如您所见,大小不再是156X121。 The height has been increased by 25%.
高度增加了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. ...向我显示了高度为“ 126”,宽度为“ 151”,与项目中的图像相匹配。 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". 我觉得奇怪的是,图像只有一个尺寸(126X151),其原始尺寸据称是1.26“ X 1.63”,缩放后的尺寸是1.57“ X 1.63”。
Who or what is allowing this 25% increase in Height? 谁或什么允许高度增加25%?
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%. 注意:如果我在图像的“大小和属性”对话框中选择“重置”按钮,它将按我的意愿缩小,将“高度”从125%设置为100%。 Is there a way to do this "Reset" programmatically?
有没有办法通过编程方式执行此“重置”操作?
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). (Workbook.Worksheets [0]无法为我编译)。
It makes no difference; 这没什么区别; the image is still stretching vertically.
图像仍在垂直拉伸。
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;"
将“ pic.Height = 121”替换为“ 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 注意: 我在Aspose担任开发人员布道者
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.