简体   繁体   English

将图像放在电子表格(Aspose Cells)上时,如何防止图像尺寸改变?

[英]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? 有没有办法通过编程方式执行此“重置”操作?

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). (Workbook.Worksheets [0]无法为我编译)。

It makes no difference; 这没什么区别; the image is still stretching vertically. 图像仍在垂直拉伸。

UPDATE 2 更新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;" 将“ 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.

相关问题 如何在电子表格范围(Aspose Cells)周围添加边框? - How can I add borders around a spreadsheet range (Aspose Cells)? 如何在计算出的像元(Aspose Cells)中防止“#Div / 0!”? - How can I prevent “#Div/0!” in calculated cells (Aspose Cells)? 如何使在多个Excel电子表格中使用的图像始终以全尺寸显示(Aspose Cells)? - How can I make an image used on multiple Excel spreadsheets always display at its full size (Aspose Cells)? 如何将电子表格中的列宽设置为特定的像素数(Aspose Cells)? - How can I set the column width in a spreadsheet to a specific pixel count (Aspose Cells)? 如何将我的数据字段放在 Excel 数据透视表(Aspose Cells)的 COLUMNS 中 - How can I place my data fields in COLUMNS in my Excel Pivot Table (Aspose Cells) 当电子表格 window 最小化时,如何获取我的 Excel VSTO 插件以更新单元格? - How can I get my Excel VSTO Add-in to update cells when the spreadsheet window is minimized? 如何将电子表格中单元格的内容输出到txt文件? - How can I output the contents of cells in my spreadsheet to a txt file? 如何设置aspose单元格以从OS中获取日期格式的语言环境? - How can I set aspose cells to take the locale for date format from OS? 如何确定数据透视表生成多少行(Aspose Cells)? - How can I determine how many rows a PivotTable generates (Aspose Cells)? 为什么Excel会更改我的日期显示值,如何防止它更改呢? - Why is Excel changing my date display value, and how can I prevent it from doing that?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM