I have a form with a Dock = Filled picture box control in it. When I resize the form's bottom or right edges, the amount of visible picture increases or decreases, but the picture's screen space coordinates remain constant. However when I resize the top or left edges of the form, the picture's image moves so that its top left edge matches the top left edge of the form.
That is, when I resize the form's left or top edges, the picture moves. This behavior makes sense for a lot of cases, and I understand why it's the default, but for my case I'd prefer if it worked like the bottom and right edges where the screen space coordinates of the image remain fixed and only more or less of it is revealed as the form is resized.
In pictures, this is the original form:
This is when it's resized by dragging the bottom right corner:
That's also what it looks like when you drag the top left corner, but in that case it's sort of dragging the picture along with it, so the picture's top left corner is always flush with the form's top left corner. But I'd rather it looked something like this mockup instead:
So that the picture is essentially "fixed" in screen space and resizing the form just reveals more or less of it.
I tried something like this:
public partial class Form1 : Form
Shown += Form1_Shown;
Resize += Form1_Resize;
void Form1_Shown(object sender, EventArgs e)
originalScreenSpace = PointToScreen(pictureBox1.Location);
private void Form1_Resize(object sender, EventArgs e)
var newLoc = PointToScreen(pictureBox1.Location);
var deltaX = newLoc.X - originalScreenSpace.X;
var deltaY = newLoc.Y - originalScreenSpace.Y;
pictureBox1.Location = new Point(-deltaX, -deltaY);
But unsurprisingly the results don't seem quite right (the image's screen space coordinates still drift) and it's far from smooth. Any ideas?
Don't use the
Dock property. Instead position the
PictureBox on the form and size it as appropriate. Then set its
Just set the Location(0, 0) and Dock the control to Left, Top:
pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left))); pictureBox1.Location = new System.Drawing.Point(0, 0);
and do not alter the Location in