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