中心与WinForms的滚动图片框 [英] Centered and scrolled PictureBox in WinForms

查看:130
本文介绍了中心与WinForms的滚动图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个WinForms应用程序,不能找出如何解决问题。 我需要显示的图像中的表格。因为图像可以任意大,我需要包含图像的图片框滚动条,使用户可以完全看到它。 周围的Googling我找到了实现这一目标的最好办法是添加图片框作为面板的子控件,使得面板autosizable和autoscrollable。 我这样做,因为编程使用设计我无法插入图片框作为面板的子控件。 我现在面临的问题是,我似乎无法能够中心和滚动图片框在同一时间。 如果我把图片框顶部,左锚,底部,右边的滚动条不显示和显示的图像是奇怪的,如果我把后面的锚只左上,图像不居中。

有没有办法在同一时间做两件事? 这里的code我的面板和图片框:

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

this.pictureBoxCapturedImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBoxCapturedImage.Location =新System.Drawing.Point(0,0);
this.pictureBoxCapturedImage.Name =pictureBoxCapturedImage;
this.pictureBoxCapturedImage.Size =新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);
 

和这里的地方我设置图像:

 的公众形象CapturedImage
{
    组
    {
        pictureBoxCapturedImage.Image =价值;
        pictureBoxCapturedImage.Size = value.Size;
    }
}
 

解决方案

对于图片框,设置 SizeMode =自动调整大小上,左,并设置其位置 0,0

设置 Panel.AutSize Panel.AutoScroll

在你的 PictureBox.Image 属性,它会自动调整大小设置为图像的大小。然后,您可以使用该大小来设置面板中的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.autoscrollposition.aspx"相对=nofollow> AutoScrollPosition 属性:

 的公众形象CapturedImage
{
    组
    {
        pictureBoxCapturedImage.Image =价值;
        panelCapturedImage.AutoScrollPosition =
            新点{
                X =(pictureBoxCapturedImage.Width  -  panelCapturedImage.Width)/ 2,
                Y =(pictureBoxCapturedImage.Height  -  panelCapturedImage.Height)/ 2
            };
    }
}
 

如果该图像是较小然后再面板的大小,它会留在左上角。如果你想让它在面板中居中,你就必须添加逻辑来设置其位置适当。

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

解决方案

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.

这篇关于中心与WinForms的滚动图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆