[英]c# Resize parent control when a child pictureBox changes the Image
I have a UserControl that contains a PictureBox and a Label. 我有一个包含图片框和标签的UserControl。 The Control loads three different images in PictureBox on different events (fore example onMouseEnter, OnMouseLeave).
控件在不同事件(例如onMouseEnter,OnMouseLeave)中在PictureBox中加载三个不同的图像。 As the images can have different sizes, I neet to resize the pictureBox and the control itself.
由于图像可以具有不同的大小,因此我无需调整pictureBox和控件本身的大小。 Below is provided the control's OnPaint event but this does not work.
下面提供了控件的OnPaint事件,但这不起作用。
protected override void OnPaint(PaintEventArgs pe)
{
if (pictureBox.Image != null)
{
this.Width = this.pictureBox.Image.Size.Width;
this.Height = this.pictureBox.Image.Size.Height;
CutRoundedRectangle2(pictureBox, cornerRadius);
}
else
{
Bitmap DrawArea = new Bitmap(pictureBox.Size.Width, pictureBox.Size.Height);
Graphics g = Graphics.FromImage(DrawArea);
Pen mypen = new Pen(Color.Black);
pictureBox.Image = DrawArea;
System.Drawing.Pen pen = new Pen(new SolidBrush(this.ForeColor));
g.DrawRectangle(pen, 0, 0, this.Width-1, this.Height-1);
g.Dispose();
}
this.labelText.ocation = new Point((this.pictureBox.Width - this.labelText.Width) / 2,
(this.pictureBox.Height - this.labelText.Height) / 2);
base.OnPaint(pe);
}
The pictureBox SizeMode is set in control's constuctor: pictureBox SizeMode在控件的构造函数中设置:
this.pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
It was long time ago when I worked with WinForms last time, but ... 很久以前,我上次使用WinForms时,但是...
My first thought is: have you tried set value of parent control's AutoSize
property to 'true' and AutoSizeMode
to GrowAndShrink
and call parent control's Refresh()
method when new image is loaded to picture box? 我的第一个想法是:您是否曾尝试将父控件的
AutoSize
属性的值设置为'true'并将AutoSizeMode
为GrowAndShrink
并在将新图像加载到图片框时调用父控件的Refresh()
方法?
@Alexey, the pictureBox resize event helped! @ Alexey,pictureBox调整大小事件很有帮助!
private void pictureBox_Resize(object sender, EventArgs e)
{
this.Width = this.pictureBox.Image.Size.Width;
this.Height = this.pictureBox.Image.Size.Height;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.