如何将图像放在电子表格(Aspose Cells)上时阻止我的图像改变大小?

时间:2016-11-30 00:34:06

标签: excel image image-scaling aspose aspose-cells

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

我把它放在纸上就像这样:

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

然而,当它在纸张上时,它会延伸并溢出到相邻的单元格中,部分模糊其他数据:

enter image description here

如您所见,尺寸不再是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

我意识到我需要&#34;工作簿&#34;是&#34; workBook&#34;由于这个原因:

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;

......但它仍然垂直地对图像进行了修饰。替换&#34; pic.Height = 121&#34;用&#34; pic.HeightScale = 100;&#34;

所以这是当前的代码,它会添加图像,但是采用垂直方式:

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;

1 个答案:

答案 0 :(得分:1)

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

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

注意: 我在Aspose担任开发人员传播者