所以我一直在研究这个问题,但是结果总是回到将BackgroundImageLayout
设置为“ Stretch”的情况。当然,对于保持原始图像的长宽比,或者在保持长宽比的情况下如何手动缩放PictureBox
或其他加载的图像的教程上,这毫无用处,但是我(对我而言非常惊讶)无法找到任何有关如何获取WinForm来管理始终位于窗口中央,被拉伸以覆盖窗口的背景图像的信息,但是却保持与原始窗口相同的纵横比。
我尝试过:
public class CustomForm : Form
{
protected Size _scale;
protected Size _originalSize;
protected float _aspectRatio;
public CustomForm() : base()
{
this.DoubleBuffered = true;
}
private float AspectRatio() { return this.AspectRatio(this._originalSize); }
private float AspectRatio(int width, int height) { return (float)width / height; }
private float AspectRatio(Size value) { return this.AspectRatio(value.Width, value.Height); }
protected override void OnBackgroundImageChanged(EventArgs e)
{
this._originalSize = new Size(this.BackgroundImage.Width, this.BackgroundImage.Height);
this._aspectRatio = this.AspectRatio(this._originalSize);
base.OnBackgroundImageChanged(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (this.BackgroundImage == null) base.OnPaintBackground(e);
else
{
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), ClientRectangle);
int x = (this._scale.Width > this.Width) ? (int)Math.Round((this._scale.Width - this.Width) / 2.0) : 0;
int y = (this._scale.Height > this.Height) ? (int)Math.Round((this._scale.Height - this.Height) / 2.0) : 0;
e.Graphics.DrawImage(this.BackgroundImage, new Rectangle(x, y, this._scale.Width, this._scale.Height));
}
}
protected override void OnSizeChanged(EventArgs e)
{
if ( this._aspectRatio > this.AspectRatio(this.Size))
{
this._scale.Width = this.Width;
this._scale.Height = (int)(this.Width / this._aspectRatio);
}
else
{
this._scale.Height = this.Height;
this._scale.Width = (int)(this.Height * this._aspectRatio);
}
base.OnSizeChanged(e);
}
}
但是,它所做的就是将图像固定在表单的左上角,并保持其基本尺寸,而不管表单的大小如何。
有人可以指出正确解决方案的方向,还是我到目前为止做错了什么?
谢谢!
答案 0 :(得分:0)
好吧,经过一番研究之后,经过一番梳理,我终于找到了答案!将其发布在这里,供以后可能会使用它的任何人!
public class CustomForm : Form
{
protected Size _scale;
protected Size _originalSize;
public CustomForm() : base()
{
this.InitializeComponent();
}
protected override void OnBackgroundImageChanged(EventArgs e)
{
this._originalSize = new Size(this.BackgroundImage.Width, this.BackgroundImage.Height);
base.OnBackgroundImageChanged(e);
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if ((BackgroundImage != null) && (BackgroundImageLayout == ImageLayout.None))
{
Point p = new Point(
(int)((this.Width - this._scale.Width) / 2),
(int)((this.Height - this._scale.Height) / 2)
);
e.Graphics.DrawImage(this.BackgroundImage, new Rectangle(p, this._scale));
}
else
base.OnPaintBackground(e);
}
protected override void OnSizeChanged(EventArgs e)
{
if ((BackgroundImage != null) && (BackgroundImageLayout == ImageLayout.None))
{
float rX = (float) this.Width / this._originalSize.Width;
float rY = (float) this.Height / this._originalSize.Height;
float r = (rX < rY) ? rY : rX;
this._scale.Height = (int)(this._originalSize.Height * r);
this._scale.Width = (int)(this._originalSize.Width * r);
}
base.OnSizeChanged(e);
}
private void InitializeComponent()
{
this._originalSize = new Size(0, 0);
this._scale = new Size(0, 0);
this.SuspendLayout();
//
// CustomForm
//
this.Name = "CustomForm";
this.DoubleBuffered = true; // minimizes "flicker" when resizing
this.ResizeRedraw = true; // forces a full redraw when resized!
this.ResumeLayout(false);
}
}