繁体   English   中英

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

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

我有一个图像嵌入到我的解决方案中,并用于Winforms应用程序的主要形式,也用于粘贴到电子表格中。 图像尺寸为156X121。

我像这样把它放在纸上:

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

但是,当它放在工作表上时,它会伸展并溢出到相邻的单元格中,从而部分掩盖了其他数据:

在此处输入图片说明

如您所见,大小不再是156X121。 高度增加了25%。 为什么? 我该如何预防呢?

这段代码:

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

...向我显示了高度为“ 126”,宽度为“ 151”,与项目中的图像相匹配。 那么为什么要更改原始大小? 我是否可以设置一个属性以保持大小不变​​而不拉伸它? 或者如何防止图像胶粘?

我觉得奇怪的是,图像只有一个尺寸(126X151),其原始尺寸据称是1.26“ X 1.63”,缩放后的尺寸是1.57“ X 1.63”。

谁或什么允许高度增加25%?

注意:如果我在图像的“大小和属性”对话框中选择“重置”按钮,它将按我的意愿缩小,将“高度”从125%设置为100%。 有没有办法通过编程方式执行此“重置”操作?

更新

根据答案,我尝试了以下方法:

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]无法为我编译)。

这没什么区别; 图像仍在垂直拉伸。

更新2

由于以下原因,我意识到我需要“工作簿”成为“工作簿”:

private static Workbook workBook;

...所以我尝试了这个:

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;

...但是它仍然可以垂直粘贴图像。 将“ pic.Height = 121”替换为“ pic.HeightScale = 100;”也是如此。

所以这是当前的代码,该代码添加了图像,但以垂直胶化的方式:

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;

请使用此代码将其重置为原始高度。

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

注意: 我在Aspose担任开发人员布道者

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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