如何防止粘贴的图像溢出边界?
How can I prevent the pasted image from overflowing its boundaries?
我将图像插入 Excel 范围,如下所示:
private System.Drawing.Image _logo;
public ProduceUsageRpt(..., System.Drawing.Image logo)
{
. . .
_logo = logo;
}
. . .
var logoRange = _xlSheet.Range[
_xlSheet.Cells[LOGO_FIRST_ROW, _grandTotalsColumn], _xlSheet.Cells[LOGO_LAST_ROW, _grandTotalsColumn]];
Clipboard.SetDataObject(_logo, true);
_xlSheet.Paste(logoRange, _logo);
很遗憾,图像对于该范围(当前为第 1 行至第 4 行,第 16 列)来说太大了。它没有做礼貌的事情并缩小自己以适应规定的范围,而是溢出并超出了指定的垂直和水平位置。
我怎样才能让图像缩小并限制在 "box" 范围内?
我在相关问题 here.
上改编了一个答案
只要我把范围设置得足够大,这个就可以了:
. . .
var logoRange = _xlSheet.Range[
_xlSheet.Cells[LOGO_FIRST_ROW, _grandTotalsColumn], _xlSheet.Cells[LOGO_LAST_ROW, _grandTotalsColumn+1]];
PlacePicture(_logo, logoRange);
}
// From Jürgen Tschandl
private void PlacePicture(Image picture, Range destination)
{
Worksheet ws = destination.Worksheet;
Clipboard.SetImage(picture);
ws.Paste(destination, false);
Pictures p = ws.Pictures(System.Reflection.Missing.Value) as Pictures;
Picture pic = p.Item(p.Count) as Picture;
ScalePicture(pic, (double)destination.Width, (double)destination.Height);
}
// Also from Jürgen Tschandl
private void ScalePicture(Picture pic, double width, double height)
{
double fX = width / pic.Width;
double fY = height / pic.Height;
double oldH = pic.Height;
if (fX < fY)
{
pic.Width *= fX;
if (pic.Height == oldH) // no change if aspect ratio is locked
pic.Height *= fX;
pic.Top += (height - pic.Height) / 2;
}
else
{
pic.Width *= fY;
if (pic.Height == oldH) // no change if aspect ratio is locked
pic.Height *= fY;
pic.Left += (width - pic.Width) / 2;
}
}
我将图像插入 Excel 范围,如下所示:
private System.Drawing.Image _logo;
public ProduceUsageRpt(..., System.Drawing.Image logo)
{
. . .
_logo = logo;
}
. . .
var logoRange = _xlSheet.Range[
_xlSheet.Cells[LOGO_FIRST_ROW, _grandTotalsColumn], _xlSheet.Cells[LOGO_LAST_ROW, _grandTotalsColumn]];
Clipboard.SetDataObject(_logo, true);
_xlSheet.Paste(logoRange, _logo);
很遗憾,图像对于该范围(当前为第 1 行至第 4 行,第 16 列)来说太大了。它没有做礼貌的事情并缩小自己以适应规定的范围,而是溢出并超出了指定的垂直和水平位置。
我怎样才能让图像缩小并限制在 "box" 范围内?
我在相关问题 here.
上改编了一个答案只要我把范围设置得足够大,这个就可以了:
. . .
var logoRange = _xlSheet.Range[
_xlSheet.Cells[LOGO_FIRST_ROW, _grandTotalsColumn], _xlSheet.Cells[LOGO_LAST_ROW, _grandTotalsColumn+1]];
PlacePicture(_logo, logoRange);
}
// From Jürgen Tschandl
private void PlacePicture(Image picture, Range destination)
{
Worksheet ws = destination.Worksheet;
Clipboard.SetImage(picture);
ws.Paste(destination, false);
Pictures p = ws.Pictures(System.Reflection.Missing.Value) as Pictures;
Picture pic = p.Item(p.Count) as Picture;
ScalePicture(pic, (double)destination.Width, (double)destination.Height);
}
// Also from Jürgen Tschandl
private void ScalePicture(Picture pic, double width, double height)
{
double fX = width / pic.Width;
double fY = height / pic.Height;
double oldH = pic.Height;
if (fX < fY)
{
pic.Width *= fX;
if (pic.Height == oldH) // no change if aspect ratio is locked
pic.Height *= fX;
pic.Top += (height - pic.Height) / 2;
}
else
{
pic.Width *= fY;
if (pic.Height == oldH) // no change if aspect ratio is locked
pic.Height *= fY;
pic.Left += (width - pic.Width) / 2;
}
}