开发人员在开发Windows窗体应用程序时,有时会为窗体设立一个背景图片,但是,不是所有的背景图片都与窗体的大小正合适,所以,就可能出现窗体中背景图片信息显示不完整等现象,那么,在c#编程语言中该如何避免这个现象的出现呢?
关键代码:
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
if (this.BackgroundImage != null)
{
// 获取当前图形对象
Graphics g = e.Graphics;
// 设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// 调整图片大小以适应窗体
var imageAttr = new ImageAttributes();
Rectangle destRect = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
g.DrawImage(this.BackgroundImage, destRect, 0, 0, this.BackgroundImage.Width, this.BackgroundImage.Height, GraphicsUnit.Pixel, imageAttr);
}
}
评论