簡體   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