将 PictureBox 保持在容器内居中

发布于 2025-01-08 03:40:59 字数 1269 浏览 0 评论 0原文

我正在设计一个简单的图片查看器,能够进行一些基本的图像处理。目前,我遇到的问题是始终将 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;
}

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

第一次就可以了

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

Form resized

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

I am designing a simple picture viewer with ability to do some basic image processing. At the moment I have the problem of keeping the PictureBox centered inside a TabPage all the time as well as keeping the picturebox width and size same as the picture its showing. So far I had no success.

I have the following code that I call in form constructor to position it in center. it works the first time to center the 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;
}

But it does not resize the picturebox to its image (you can see the black part that is back color for the picturebox control):

IT is ok the first time

The problem is getting worse as soon as I resize the form, the picturebox position will goes to top:

Form resized

I call the code above in form's resize event as well, no idea why it works when application starts. Would be nice if someone can tell me what properties I should take care of to achieve a nicely centered picturebox which always is as big as its image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥琐帝 2025-01-15 03:41:00

我相信你的问题就在这里

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;

I believe your problem lies here

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 is alwways set to tabImageView Y, althought it should be set to

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

should be almost the same with xPoint

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

and

width = picBoxView.Image.Width;
height = picBoxView.Image.Height;
倾城°AllureLove 2025-01-15 03:40:59

如果您只需将 Anchor 样式设置为 none,这非常简单:

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

It's pretty easy if you just set the Anchor style to none:

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

Then just center the PictureBox initially whenever you change the image of the 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();
}

Having the Anchor = None will center the PictureBox control for you whenever the parent container gets resized because it "isn't" anchored to the default Left and Top locations.

酷遇一生 2025-01-15 03:40:59

给定一个带有 TabControlForm,其中 Dock 设置为 Fill,下面将保留您的 PictureBox< /code> 在中心。它还将 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);

        }
    }

Givien a Form with TabControl, which has Dock set to Fill, below will keep your PictureBox in the centre. It also sets PictureBox size to Bitmap size:

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

        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文