2012-02-21 34 views
7

我正在设计一个简单的图片浏览器,能够做一些基本的图像处理。目前我有问题始终将PictureBox集中在TabPage之内,并且保持picturebox的宽度和尺寸与显示的图片相同。到目前为止,我没有成功。在容器内保持一个图片框居中

我有以下代码,我在窗体构造函数中调用它的位置在中心。它工作在第一时间到中心在PictureBox:

private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None) 
{ 
    if (makeImageNull) picBoxView.Image = null; 
    picBoxView.Dock = dockStyle; 

    var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width/2)/2); 
    var yPoint = tabImageView.Location.Y; 

    var width = tabImageView.Width/2; 
    var height = (tabImageView.Height/2) - toolStripImageView.Height; 

    if (picBoxView.Image == null) return; 

    //Resize image according to width 
    picBoxView.Image = ImageMethods.ResizeImage(picBoxView.Image.Tag.ToString(), width, height, false); 

    picBoxView.Location = new Point(xPoint, yPoint); 
    picBoxView.Width = width; 
    picBoxView.Height = height; 
} 

不过,这并不调整PictureBox的自己的形象(你可以看到黑色的部分是背景色为PictureBox控件):

IT is ok the first time

的问题是,只要我调整的形式越来越差,PictureBox的位置会去顶:

Form resized

我也在form的resize事件中调用了上面的代码,不知道为什么它在应用程序启动时工作。如果有人能告诉我应该关注哪些属性来创建一个中心完美的图画盒,那么它总是和图像一样大。

+0

尝试设置Dock以填充图像并将其设置为背景图像。 BackgroundImageLayout可以设置为Center。并将BackGroundColor设置为透明 – 2012-02-21 10:15:03

+0

“picturebox.Image”大小是否等于“picturebox”大小?因为我需要处理图像的像素,我不知道图像盒是否大于图像,我正在处理的图像将是图像盒本身?!听起来很愚蠢,我知道! – 2012-02-21 10:18:49

+0

但是如果我想放大照片,我想我会遇到问题! – 2012-02-21 10:22:54

回答

14

,如果你刚才设置的Anchor风格没有这是很容易:

picBoxView = new PictureBox(); 
picBoxView.SizeMode = PictureBoxSizeMode.AutoSize; 
picBoxView.Anchor = AnchorStyles.None; 
tabImageView.Controls.Add(picBoxView); 
CenterPictureBox(picBoxView, myImage); 

然后,只需居中PictureBox最初每当变更PictureBox的图像:

private void CenterPictureBox(PictureBox picBox, Bitmap picImage) { 
    picBox.Image = picImage; 
    picBox.Location = new Point((picBox.Parent.ClientSize.Width/2) - (picImage.Width/2), 
           (picBox.Parent.ClientSize.Height/2) - (picImage.Height/2)); 
    picBox.Refresh(); 
} 

拥有Anchor = None会分呃每当父容器被调整大小,因为它“不是”锚定到默认的左侧和顶部位置时,您可以控制PictureBox

1

Givien一个FormTabControl,其中有Dock设置为Fill,下面将让您的PictureBox在中心。它还设置PictureBox大小Bitmap尺寸:

public partial class Form1 : Form 
    { 
     Bitmap b = new Bitmap(320, 200); 
     public Form1() 
     { 
      InitializeComponent(); 
      CenterTheBox(); 
     } 

     private void Form1_Resize(object sender, EventArgs e) 
     { 
      CenterTheBox(); 
     } 

     void CenterTheBox() 
     { 
      pictureBox1.Size = b.Size; 
      var left = (tabPage1.ClientRectangle.Width - pictureBox1.ClientRectangle.Width)/2; 
      var top = (tabPage1.ClientRectangle.Height - pictureBox1.ClientRectangle.Height)/2; 
      pictureBox1.Location = new Point(tabPage1.ClientRectangle.Location.X + left, tabPage1.ClientRectangle.Location.Y + top); 

     } 
    } 
1

我相信你的问题就出在这里

var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width/2)/2); 
var yPoint = tabImageView.Location.Y; 

var width = tabImageView.Width/2; 
var height = (tabImageView.Height/2) - toolStripImageView.Height; 

ypoint被alwways设置为tabImageView Y,althought应该设置为

​​

应与xPoint几乎相同

tabImageView.Location.X + (tabImageView.Size.Width - picBoxView.Size.Width)/2 

width = picBoxView.Image.Width; 
height = picBoxView.Image.Height;