WinForms 中居中和滚动的 PictureBox
我正在开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于
PictureBox
,设置SizeMode = AutoSize
,锚定
它Top,Left
,并设置其Location
到0, 0
。将
Panel.AutSize
设置为False
,并将Panel.AutoScroll
设置为True
。当您设置
PictureBox.Image
属性时,它将自动调整图像的大小。然后,您可以使用该大小来设置面板的 AutoScrollPosition 属性:如果图像小于面板的大小,它将保留在左上角。如果您希望它在面板中居中,则必须添加逻辑来正确设置其
Location
。For the
PictureBox
, setSizeMode = AutoSize
,Anchor
itTop, Left
, and set itsLocation
to0, 0
.Set
Panel.AutSize
toFalse
andPanel.AutoScroll
toTrue
.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: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.根据之前的答案,我能够创建这个完整的示例:
Based on earlier answers I was able to create this full example:
图片框必须设置为自动调整大小。锚定在中心(或边界)。
您可以在设计器中管理所有这些,但不要理解您的问题。
面板必须设置为自动滚动为 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.