【问题标题】:Keeping a PictureBox centered inside a container保持 PictureBox 在容器内居中
【发布时间】:2012-02-21 10:06:46
【问题描述】:

我正在设计一个能够进行一些基本图像处理的简单图片查看器。目前我遇到的问题是始终将PictureBox 居中在TabPage 内,并保持图片框的宽度和大小与其显示的图片相同。到目前为止,我没有成功。

我在表单构造函数中调用了以下代码以将其定位在中心。它第一次使图片框居中:

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

但它不会将图片框调整为其图像的大小(您可以看到图片框控件的背景颜色为黑色的部分):

一旦我调整表单大小,问题就会变得更糟,图片框的位置会移到顶部:

我在表单的 resize 事件中也调用了上面的代码,不知道为什么它在应用程序启动时起作用。如果有人能告诉我应该注意哪些属性来实现一个居中且始终与其图像一样大的图片框,那就太好了。

【问题讨论】:

  • 尝试将Dock设置为填充并将图像设置为背景图像。 BackgroundImageLayout 可以设置为Center。并且背景颜色为透明
  • picturebox.Image 的大小是否等于picturebox 的大小?因为我需要处理图像的像素,我不知道图片框是否大于图片,我正在处理的图像将是图片框本身?!我知道听起来很愚蠢!
  • 但是我想如果我想放大图片我会遇到问题!

标签: c# winforms picturebox


【解决方案1】:

如果您将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 控件,因为它“没有”锚定到默认的左侧和顶部位置。

【讨论】:

    【解决方案2】:

    给Form 和TabControl,其中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);
    
            }
        }
    

    【讨论】:

      【解决方案3】:

      我相信你的问题就在这里

      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 总是设置为 tabImageView Y,尽管它应该设置为

      tabImageView.Location.Y + (tabImageView.Size.Height - picBoxView.Size.Height)/2
      

      应该和xPoint差不多

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

      和

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多