图像缩放Title、Zoom、Stretch的实现方式

这篇文章记录图片缩放中几种常见的方式,对应C#中ImageLayout中的几种枚举。

1.None(将原图绘制以左上角为原点绘制)

e.Graphics.DrawImageUnscaled(this.ContentImage, new Point(0, 0));
2.Title(平铺)

int x = 0, y = 0;
while (y < e.ClipRectangle.Height)
{
    e.Graphics.DrawImageUnscaled(this.ContentImage, new Point(x, y));
    x += this.ContentImage.Width;
    if (x >= e.ClipRectangle.Width)
    {
        x = 0;
        y += this.ContentImage.Height;
    }
}
3.Zoom(最适应)

float scaleX = e.ClipRectangle.Width * 1.0F / this.ContentImage.Width;
float scaleY = e.ClipRectangle.Height * 1.0F / this.ContentImage.Height;
RectangleF rectF = new RectangleF();
if (scaleX < scaleY)
{
    rectF.Width = this.ContentImage.Width * scaleX;
    rectF.Height = this.ContentImage.Height * scaleX;
}
else
{
    rectF.Width = this.ContentImage.Width * scaleY;
    rectF.Height = this.ContentImage.Height * scaleY;
}
rectF.X = (e.ClipRectangle.Width - rectF.Width) / 2.0F;
rectF.Y = (e.ClipRectangle.Height - rectF.Height) / 2.0F;
e.Graphics.DrawImage(this.ContentImage, rectF);
4.Stretch(拉伸填充)

e.Graphics.DrawImage(this.ContentImage, e.ClipRectangle);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值