这篇文章记录图片缩放中几种常见的方式,对应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);