Picturebox不以Form调整大小为中心

时间:2013-03-10 14:23:46

标签: c# .net winforms image-processing designer

我正在使用C#窗体,我无法重新定位Picturebox图像。

我要做的是查看显示器的分辨率并将表单设置为全屏,然后将图片框设置为表格内的全屏。

在设计器中将Windowstate设置为Maximized,在Designer中将FormBorderstyle设置为None。

    void Monitorresolution()
    {
        // grabs the resolution of the monitor
        Screen screen = Screen.PrimaryScreen;
        screenWidth = screen.Bounds.Width;
        screenHeight = screen.Bounds.Height;
        //MessageBox.Show("height = " + screenWidth + "/n" + "Width = " + screenHeight);
        // grabs the resolution of the monitor

        // sets the size of the window of Pictureviewer
        this.ClientSize = new Size(screenWidth, screenHeight);
        // sets the size of the window of Pictureviewer

        // sets the size of the picturebox
        pictureBox1.Size = new Size(screenWidth, screenHeight);
        // sets the size of the picturebox

        // sets the size of the image inside picturebox
        pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
        // sets the size of the image inside picturebox

        // center the image
        pictureBox1.Location = new Point((pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Width / 2), (pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Height / 2));
        // center the image


        // anchors the picturebox
        pictureBox1.Anchor = AnchorStyles.None;

        // MessageBox.Show("WTF");

        pictureBox1.Refresh();       
    }

图像将偏离中心,除非我删除评论任何消息框语句。 打开消息框后,图像将完美居中。

为什么会发生这种情况,我该怎么做才能解决它。

由于 安迪


更新

我正在展示更多代码,以便更好地了解我正在做什么以及为什么pictureBox1.Dock = DockStyle.Fill; 在这种情况下,对我不起作用。

我稍微重新排序了代码并将调整大小代码放在form_load代码中。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Image_Viewer
{
public partial class imageViewer : Form
{
    System.Drawing.Point mouseDown;

    int newWidth = 0;
    int newHeight = 0;
    int newX = 0;
    int newY = 0;

    int screenWidth;
    int screenHeight;


    public imageViewer()
    {
        InitializeComponent();

        var frm2 = new exitform();
        frm2.FormClosed += (o, e) => this.Close();
        frm2.Show();
    }

    private void imageViewer_FormClosed(object sender, FormClosedEventArgs e)
    {

    }

    private void imageViewer_Load(object sender, EventArgs e)
    {
        using (OpenFileDialog dlg = new OpenFileDialog())
        {
            dlg.Title = "Open Image";
            dlg.Filter = "JPG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff|BMP|*.bmp|GIF|*.gif|" + "All Graphics Types|*.bmp;*.jpg;*.jpeg;*.png;*.tif;*.tiff";

            if (dlg.ShowDialog() == DialogResult.OK)
            {

                using (var fs = new System.IO.FileStream(dlg.FileName, System.IO.FileMode.Open))
                {
                    var bmp = new Bitmap(fs);
                    pictureBox1.Image = bmp;
                    Invalidate();
                }
            }
        }

        // grabs the resolution of the monitor
        Screen screen = Screen.PrimaryScreen;
        screenWidth = screen.Bounds.Width;
        screenHeight = screen.Bounds.Height;
        // MessageBox.Show("height = " + screenHeight + "\n" + "Width = " + screenWidth);
        // grabs the resolution of the monitor


        // sets the size of the window of Pictureviewer
        this.ClientSize = new Size(screenWidth, screenHeight);
        // sets the size of the window of Pictureviewer

        pictureBox1.Size = new Size(screenWidth, screenHeight);

        pictureBox1.Location = new Point((ClientSize.Width / 2) - (pictureBox1.Width / 2), (ClientSize.Height / 2) - (pictureBox1.Height / 2));

        // MessageBox.Show("When this message box pops, everything seems to work, no idea why though");
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Left)
        {
            mouseDown = mouse.Location;
            pictureBox1.Dock = DockStyle.None;
        }

        else if (mouse.Button == MouseButtons.Right)
        {
            // Do something else, not important in this example
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Left)
        {
            // Pan functions
            Point mousePosNow = mouse.Location;

            int deltaX = mousePosNow.X - mouseDown.X;
            int deltaY = mousePosNow.Y - mouseDown.Y;

            int newX = pictureBox1.Location.X + deltaX;
            int newY = pictureBox1.Location.Y + deltaY;

            pictureBox1.Location = new Point(newX, newY);
        }

        else if (mouse.Button == MouseButtons.Right)
        {
            this.Close();
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        MouseEventArgs mouse = e as MouseEventArgs;

        if (mouse.Button == MouseButtons.Left)
        {
            Point mousePosNow = mouse.Location;

            int deltaX = mousePosNow.X - mouseDown.X;
            int deltaY = mousePosNow.Y - mouseDown.Y;

            int newX = pictureBox1.Location.X + deltaX;
            int newY = pictureBox1.Location.Y + deltaY;

            pictureBox1.Location = new Point(newX, newY);
        }
    }

    protected override void OnMouseWheel(MouseEventArgs e)//zoom function
    {
        if (e.Delta > 0)
        {
            newWidth = pictureBox1.Size.Width + (pictureBox1.Size.Width / 10);
            newHeight = pictureBox1.Size.Height + (pictureBox1.Size.Height / 10);

            newX = pictureBox1.Location.X - ((pictureBox1.Size.Width / 10) / 2);
            newY = pictureBox1.Location.Y - ((pictureBox1.Size.Height / 10) / 2);
        }

        else if (e.Delta < 0)
        {
            newWidth = pictureBox1.Size.Width - (pictureBox1.Size.Width / 10);
            newHeight = pictureBox1.Size.Height - (pictureBox1.Size.Height / 10);
            newX = pictureBox1.Location.X + ((pictureBox1.Size.Width / 10) / 2);
            newY = pictureBox1.Location.Y + ((pictureBox1.Size.Height / 10) / 2);
        }

        pictureBox1.Size = new Size(newWidth, newHeight);
        pictureBox1.Location = new Point(newX, newY);
    }

}
}

表单2使用以下代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Image_Viewer
{
public partial class exitform : Form
{
    private const int CP_NOCLOSE_BUTTON = 0x200;

    public exitform()
    {
        InitializeComponent();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams myCp = base.CreateParams;
            myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
            return myCp;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
}

我只显示表单2的代码只是为了显示一个退出按钮。我将它显示在表单1的顶部,然后从两者中删除关闭按钮。

AppDeveloper我确实尝试了你发布的代码,但是在我的情况下它不起作用,我将以较小的形式重试以进行测试。我发现奇怪的是,它出于某种原因在消息框之后工作。

由于 安迪

4 个答案:

答案 0 :(得分:2)

您应该将pictureBox1位置置于其托管的Form的中心位置(即家长控制/表格)。

我不确定为什么要将图像置于PrimaryScreen

的中心位置

将PictureBox居中的更好方法是订阅Form.Resize事件并动态调整pixtureBox1位置。

private void Form1_Load(object sender, EventArgs e)
{
    this.Resize += Form1_Resize;
}

private void Form1_Resize(object sender, EventArgs e)
{
    pictureBox1.Left = (this.Width - pictureBox1.Width)/2;
    pictureBox1.Top = (this.Height - pictureBox1.Height)/2;
}

答案 1 :(得分:2)

正如我所见,您正在设置picturebox的大小=表单的大小。在你的情况下,最好只设置

pictureBox1.Dock = DockStyle.Fill;

忘记设置尺寸/适当调整大小:)

答案 2 :(得分:0)

我更喜欢C ++ CLI,但下一个例子应该是明确的。

    Void MyForm::SetFullScreen(Boolean value)
    {
        if (value == FullScreen)
            return;

        if (value)
        {
            // DO NOT REORDER!!!
            this->LastWindowState = WindowState;                
            this->WindowState = FormWindowState::Normal;
            this->LastWindowBounds = Bounds;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;                              
            this->Bounds = Screen::PrimaryScreen->Bounds;
        }
        else
        {
            // DO NOT REORDER!!!
            this->WindowState = LastWindowState;
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Sizable;
            this->Bounds = LastWindowBounds;                    
        }

         Int32 NewX = (pictureBox1.Parent.ClientSize.Width / 2) - (pictureBox1.Width / 2);
         Int32 NewY = (pictureBox1.Parent.ClientSize.Height / 2) - (pictureBox1.Height / 2);
         pictureBox1.Location = new Point(NewX, NewY);
         pictureBox1.Anchor = AnchorStyles.None; 

        // finally save the actual state
        FullScreen = value;
    }

答案 3 :(得分:0)

我终于找到了问题所在

我错过了码头风格。如果我将其插入

下面
pictureBox1.Location = new Point((ClientSize.Width / 2) - (pictureBox1.Width / 2), (ClientSize.Height / 2) - (pictureBox1.Height / 2));

pictureBox1.Dock = DockStyle.Top;

每次似乎都很完美。

感谢所有为我看过这个的人。

安迪