我有一个包含PictureBox和Label的UserControl。 Control在不同事件上加载PictureBox中的三个不同图像(例如onMouseEnter,OnMouseLeave)。由于图像可以有不同的大小,我需要调整pictureBox的大小 控制本身。下面提供了控件的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在控件的构造函数中设置:
this.pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
答案 0 :(得分:1)
很久以前,我上次与WinForms合作过,但是......
我的第一个想法是:您是否尝试将父控件的AutoSize
属性的值设置为'true',将AutoSizeMode
设置为GrowAndShrink
并在加载新图像时调用父控件的Refresh()
方法到图片框?
答案 1 :(得分:0)
@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;
}