WinForms 中居中和滚动的 PictureBox

发布于 2024-12-10 10:13:09 字数 1674 浏览 2 评论 0原文

我正在开发 WinForms 应用程序,但不知道如何解决问题。 我需要在表单中显示图像。因为图像可以任意大,所以我需要在包含图像的图片框上有滚动条,以便用户可以完整地看到它。 谷歌搜索后,我发现实现此目的的最佳方法是将 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;
    }
}

I'm developing a WinForms application and can't figure out how to resolve an issue.
I need to show an image in a Form. Because the image can be arbitrarily large, I need scrollbars on the picturebox containing the image so the user can see it entirely.
Googling around I found out the best way to achieve this is to add the PictureBox as a Child Control of a Panel, and making the Panel autosizable and autoscrollable.
I did that programatically since using the designer I was unable to insert the picturebox as a child control of the panel.
The problem I'm now facing is that I can't seem to be able to center and scroll the picturebox at the same time.
If I put the anchor of the picturebox to top,left,bottom,right, the scrollbars are not shown and the image displayed is strange, if I put back the anchor to only top-left, the image is not centered.

Is there any way to do both at the same time?
Here's the code for my Panel and 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);

And here's where I set the image:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

临走之时 2024-12-17 10:13:09

对于PictureBox,设置SizeMode = AutoSize锚定Top,Left,并设置其Location0, 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

For the PictureBox, set SizeMode = AutoSize, Anchor it Top, Left, and set its Location to 0, 0.

Set Panel.AutSize to False and Panel.AutoScroll to True.

When you set the PictureBox.Image property, it will auto-size to the size of the image. You can then use that size to set the panel's AutoScrollPosition property:

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

If the image is smaller then then panel's size, it will remain in the upper left corner. If you want it centered within the panel, you'll have to add logic to set its Location appropriately.

一身骄傲 2024-12-17 10:13:09

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

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

Based on earlier answers I was able to create this full example:

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();
   }
前事休说 2024-12-17 10:13:09

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

您可以在设计器中管理所有这些,但不要理解您的问题。

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

The picturebox has to be set to autosize. anchored at the center (or a border).

You could manage all of this in the designer, don't undertand your problem with that.

The panel has to be set to autoscroll to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文