Andy Dansby
Andy Dansby

Reputation: 25

Picturebox not Centering on Form Resize

I am working on a C# windows form and I am having trouble re-centering the Picturebox image.

What I am looking to do is look at the resolution of the monitor and set the form to a full screen and then set the picturebox as a full screen within the form.

Windowstate is set to Maximized in designer, FormBorderstyle is set to None in Designer.

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

The image will be off center, unless I remove the comment any of the messagebox statements. With the messagebox on, the image is centered perfectly.

Why is this happening and what can I do to fix it.

Thanks Andy


UpDate

I am showing more of the code to get a better picture of what I am doing and why pictureBox1.Dock = DockStyle.Fill; will not work for me in this case.

I have reordered the code a bit and placed the resize code in the form_load code.


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

}
}

Form 2 uses the following code

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

I am only showing form 2's code just to show an exit button. I show it on top of form 1 and remove the close button from both.

AppDeveloper I did try the code you posted however it did not work in my case, I will retry in a smaller form for testing purposes. What I find strange though is it does for some reason work after the messagebox.

Thanks Andy

Upvotes: 0

Views: 5908

Answers (4)

Andy Dansby
Andy Dansby

Reputation: 25

I finally found what the problem was

I was missing the dock style. If I insert it just under the

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

pictureBox1.Dock = DockStyle.Top;

It seems to work perfectly every time.

Thanks to everyone who looked at this for me.

Andy

Upvotes: 1

Zoltan Tirinda
Zoltan Tirinda

Reputation: 799

I prefer C++ CLI, but the next example should be clear.

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

Upvotes: 0

VladL
VladL

Reputation: 13033

As I see, you are setting the size of the picturebox = size of the form. In your case it's better to just set

pictureBox1.Dock = DockStyle.Fill;

and forget about setting size/proper resizing :)

Upvotes: 2

Parimal Raj
Parimal Raj

Reputation: 20575

You should center the pictureBox1 location with respect to the Form it is hosted on (i.e. Parent Control/Form).

I am not sure why you are centering the image with respect to the PrimaryScreen

A better way to center the pictureBox would be to Subscribe to the Form.Resize event and adjust the pixtureBox1 location dynamically.

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

Upvotes: 2

Related Questions