C# 尝试设置始终覆盖窗体但具有静态/固定纵横比的背景图像
所以我一直在仔细研究这个问题,但结果总是回到C# 尝试设置始终覆盖窗体但具有静态/固定纵横比的背景图像,c#,winforms,image-processing,C#,Winforms,Image Processing,所以我一直在仔细研究这个问题,但结果总是回到BackgroundImageLayout设置为“拉伸”的位置。当然,这对保持原始图像的纵横比没有任何作用,也对如何在保持纵横比的同时手动缩放PictureBox或其他加载图像的教程没有任何作用,但我不能(对我来说非常惊讶)了解如何使用WinForm管理始终以窗口为中心、拉伸以覆盖窗口、但保持与原始图像相同纵横比的背景图像 我试过这个: public class CustomForm : Form { protected Size _scale
BackgroundImageLayout
设置为“拉伸”的位置。当然,这对保持原始图像的纵横比没有任何作用,也对如何在保持纵横比的同时手动缩放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);
}
}
但它所做的只是将图像固定在表单左上角的基本尺寸上,而不管表单的大小如何调整
有人能告诉我一个正确的解决方法,或者我到目前为止做错了什么
谢谢 好吧,经过一系列的研究,我终于把问题解决了!为将来可能来找它的任何人发布此信息
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);
}
}
公共类自定义表单:表单
{
保护尺寸(单位刻度);;
保护尺寸_原始尺寸;
public CustomForm():base()
{
this.InitializeComponent();
}
BackgroundImageChanged上受保护的覆盖无效(EventArgs e)
{
this.\u originalSize=新尺寸(this.BackgroundImage.Width,this.BackgroundImage.Height);
根据背景图像(e);
}
PaintBackground上受保护的覆盖无效(PaintEventArgs e)
{
如果((BackgroundImage!=null)和&(BackgroundImageLayout==ImageLayout.None))
{
点p=新点(
(int)((此宽度-此刻度宽度)/2),
(int)((此高度-此刻度高度)/2)
);
e、 Graphics.DrawImage(this.BackgroundImage,新矩形(p,this._比例));
}
其他的
根据背景(e);
}
IzeChanged上的受保护覆盖无效(EventArgs e)
{
如果((BackgroundImage!=null)和&(BackgroundImageLayout==ImageLayout.None))
{
float rX=(float)this.Width/this.\u originalSize.Width;
float rY=(float)this.Height/this.\u originalSize.Height;
浮点数r=(rX