在WinForms中居中并滚动PictureBox

时间:2011-10-17 19:25:12

标签: c# .net winforms scrollbar picturebox

我正在开发一个WinForms应用程序,无法弄清楚如何解决问题。 我需要在表单中显示图像。因为图像可以任意大,我需要包含图像的图片框上的滚动条,以便用户可以完全看到它。 谷歌搜索我发现实现这一目标的最佳方法是将PictureBox添加为Panel的子控件,并使Panel可自动调整和自动滚动。 我以编程方式做了这个,因为使用设计师我无法插入图片框作为面板的子控件。 我现在面临的问题是,我似乎无法同时居中并滚动图片框。 如果我将图片框的锚点放在顶部,左侧,底部,右侧,则不会显示滚动条,显示的图像很奇怪,如果我将锚点放回到左上角,则图像不会居中。

有没有办法同时做两件事? 这是我的Panel和Picturebox的代码:

this.panelCapturedImage = new System.Windows.Forms.Panel();
this.panelCapturedImage.SuspendLayout();
this.panelCapturedImage.AutoScroll = true;
this.panelCapturedImage.AutoSize = true;
this.panelCapturedImage.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.panelCapturedImage.Controls.Add(this.pictureBoxCapturedImage);
this.panelCapturedImage.Location = new System.Drawing.Point(0, 49);
this.panelCapturedImage.Name = "panelCapturedImage";
this.panelCapturedImage.Size = new System.Drawing.Size(3, 3);
this.panelCapturedImage.TabIndex = 4;

this.pictureBoxCapturedImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBoxCapturedImage.Location = new System.Drawing.Point(0, 0);
this.pictureBoxCapturedImage.Name = "pictureBoxCapturedImage";
this.pictureBoxCapturedImage.Size = new System.Drawing.Size(0, 0);
this.pictureBoxCapturedImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
this.pictureBoxCapturedImage.TabIndex = 0;
this.pictureBoxCapturedImage.TabStop = false;

this.panelCapturedImage.Controls.Add(this.pictureBoxCapturedImage);

这是我设置图像的地方:

public Image CapturedImage
{
    set 
    { 
        pictureBoxCapturedImage.Image = value;
        pictureBoxCapturedImage.Size = value.Size;
    }
}

3 个答案:

答案 0 :(得分:4)

对于PictureBox,设置SizeMode = AutoSizeAnchorTop, Left,并将其Location设置为0, 0

Panel.AutSize设为False,将Panel.AutoScroll设为True

设置PictureBox.Image属性时,它会自动调整大小为图像大小。然后,您可以使用该大小设置面板的AutoScrollPosition属性:

public Image CapturedImage
{
    set 
    { 
        pictureBoxCapturedImage.Image = value;
        panelCapturedImage.AutoScrollPosition = 
            new Point { 
                X = (pictureBoxCapturedImage.Width - panelCapturedImage.Width) / 2, 
                Y = (pictureBoxCapturedImage.Height - panelCapturedImage.Height) / 2 
            };
    }
}

如果图像小于面板尺寸,它将保留在左上角。如果您希望它在面板中居中,则必须添加逻辑以正确设置其Location

答案 1 :(得分:1)

根据之前的答案,我能够创建这个完整的例子:

private void testShowPictureBox()
    {
        /* format form */
        Form frmShowPic = new Form();
        frmShowPic.Width = 234;
        frmShowPic.Height = 332;
        frmShowPic.MinimizeBox = false;
        frmShowPic.MaximizeBox = false;
        frmShowPic.ShowIcon = false;
        frmShowPic.StartPosition = FormStartPosition.CenterScreen;

        frmShowPic.Text = "Show Picture";

        /* add panel */
        Panel panPic = new Panel();
        panPic.AutoSize = false;
        panPic.AutoScroll = true;
        panPic.Dock = DockStyle.Fill;

        /* add picture box */
        PictureBox pbPic = new PictureBox();
        pbPic.SizeMode = PictureBoxSizeMode.AutoSize;
        pbPic.Location = new Point(0, 0);

        panPic.Controls.Add(pbPic);
        frmShowPic.Controls.Add(panPic);

        /* define image */
        pbPic.ImageLocation = @"c:\temp\pic.png";

        frmShowPic.ShowDialog();
   }

答案 2 :(得分:0)

必须将图片框设置为自动调整大小。锚定在中心(或边界)。

您可以在设计师中管理所有这些,不要对此表示不满。

必须将面板设置为自动滚动到true。