当一个孩子的PictureBox改变图像C#调整大小母公司控制(c# Resize parent c

2019-09-19 15:56发布

我有一个包含一个PictureBox和标签的用户控件。 控制上的不同事件加载在图片框三个不同的图像(例如前OnMouseEnter在,OnMouseLeave在)。 由于图像可以有不同的尺寸,我NEET来调整图片框和控制本身。 下面提供了控件的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);
    }

PictureBox的SizeMode设置控制的constuctor:

this.pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;

Answer 1:

这是很久以前,当我与WinForms的工作最后一次,但...
我首先想到的是:你尝试设置父控件的值AutoSize属性为“真”和AutoSizeModeGrowAndShrink和呼叫父控件的Refresh()当新的图像加载到图片框的方法?



Answer 2:

@Alexey,在PictureBox resize事件帮助!

    private void pictureBox_Resize(object sender, EventArgs e)
    {
        this.Width = this.pictureBox.Image.Size.Width;
        this.Height = this.pictureBox.Image.Size.Height;
    }


文章来源: c# Resize parent control when a child pictureBox changes the Image