【发布时间】:2011-12-09 13:20:04
【问题描述】:
我正在开发 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;
}
}
【问题讨论】:
标签: c# .net winforms scrollbar picturebox