FarPoint 表格(居中)自适应显示图片

FarPoint 表格自适应显示图片,最简单的就是用 Graphic 重绘图片,把图片重绘成表格的大小。但是对于需要打印的文件,重绘会使得图片失真而质量下降。

Image image = Image.FromFile("image.png");// 原图
int myWidth = 100;// 想要的宽
int myHeight = 50;// 想要的高
Image bitmap = new System.Drawing.Bitmap(myWidth, myHeight);
using (Graphics g = System.Drawing.Graphics.FromImage(bitmap))
{
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
    g.Clear(Color.Transparent);
    g.DrawImage(image, new Rectangle(0, 0, myWidth, myHeight), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
}
Image newImage = bitmap;// 自定义大小的图片

FarPoint 表格自适应显示图片,图片自适应cell表格,不会导致图片失真

Image image = Image.FromFile("image.png");
FarPoint.Win.Spread.CellType.ImageCellType imgCellType = new FarPoint.Win.Spread.CellType.ImageCellType();
imgCellType.Style = FarPoint.Win.RenderStyle.StretchAndScale;// 图片自适应,等比例缩放
cell.CellType = imgCellType;// cell 为 FarPoint.Win.Spread.Cell格式
cell.Value = image;

如果要在cell表格居中显示图片,设置 cell.HorizontalAlignment 和 cell.VerticalAlignment 等于 Center 是不起作用的,因为这两个属性主要针对的对象是 Text,而不是图片。需要用借助 FarPoint.Win.Picture 工具:

FarPoint.Win.Picture picture = new FarPoint.Win.Picture();
picture.Image = image;
picture.AlignHorz = FarPoint.Win.HorizontalAlignment.Center;
picture.AlignVert = FarPoint.Win.VerticalAlignment.Center;
picture.Style = FarPoint.Win.RenderStyle.StretchAndScale;// 图片自适应,等比例缩放
FarPoint.Win.Spread.CellType.TextCellType tCellType = new FarPoint.Win.Spread.CellType.TextCellType();
tCellType.BackgroundImage = picture;
cell.CellType = tCellType;

你可能感兴趣的:(C#)