居中和滚动的图片框在WinForms

本文关键字:WinForms 滚动 | 更新日期: 2023-09-27 18:11:25

我正在开发一个WinForms应用程序,不知道如何解决一个问题。我需要显示在一个形式的图像。因为图像可以任意大,所以我需要在包含图像的picturebox上设置滚动条,以便用户可以完全看到图像。通过谷歌搜索,我发现实现这一目标的最佳方法是将PictureBox添加为面板的子控件,并使面板可自动缩放和自动滚动。由于使用设计器,我无法将图片框插入为面板的子控件,因此我以编程方式完成了该操作。我现在面临的问题是,我似乎无法同时将居中并滚动。如果我把图片框的锚点放在顶部、左侧、底部、右侧,滚动条不会显示,显示的图像是奇怪的,如果我把锚点放回左上角,图像不是居中的。

有没有办法同时做到这两点?下面是我的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;
    }
}

居中和滚动的图片框在WinForms

设置PictureBoxSizeMode = AutoSize, AnchorTop, Left, Location0, 0

设置Panel.AutSizeFalse, Panel.AutoScrollTrue

当您设置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

基于之前的回答,我创建了这个完整的例子:

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();
   }

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

你可以在设计器中管理所有这些,我不明白你的问题所在。

面板必须设置为自动滚动为真。