【问题标题】:How can I prevent my image from changing size when placing it on a spreadsheet (Aspose Cells)?将图像放在电子表格(Aspose Cells)上时,如何防止图像大小发生变化?
【发布时间】:2016-11-30 00:34:05
【问题描述】:

我的解决方案中嵌入了一张图片,用于 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%?

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

更新

根据答案,我尝试了这个:

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.HeightScale = 100;”替换“pic.Height = 121”也是如此

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

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;

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 那不会为我编译; “工作簿”没有“工作表”属性……但确实如此: Picture pic = pivotTableSheet.Pictures[0];我会看看它是否有效......
    • 它应该可以编译,但您需要稍微修改一下代码。对于 wb,您应该使用自己的工作簿变量。假设您的工作簿名称是 myWb,那么它将看起来像 myWb.Worksheets[0].Pictures[0] 等。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多