Reputation: 303
In Window Forms, I've put a pictureBox as a background image. Then, I've made it so that if I resize the window, the image stretches and gets bigger as I resize it, and that the entire picture is always filling the entire form. For this, I've used the following code:
image.Dock = DockStyle.Fill; image.Anchor = AnchorStyles.Top | AnchorStyles.Left; image.SizeMode = PictureBoxSizeMode.StretchImage;
I had no problems with that whatsoever simply because the image is filling in the entire window.
What I want to do now, however, is to put on another (smaller) pictureBox, that's going to be exactly 20 px above the center of the Form, and will also stretch appropriately as I resize the window, and will always stay slightly above the center of the Form, no matter how much I resize it.
The thing is that I cannot set the Location property of the pictureBox, because then the position is absolute and the image will not stretch and stay in the center as I resize the window, but will remain where it is, according to the x and y coordinates of the window. What I want is for the picture to stretch and move according to how I stretch and move the window, just like I've managed to do with the image I have in background.
Upvotes: 0
Views: 4874
Reputation: 6343
Position your imagebox on the form where you want it, then set the Anchor proprety to top, bottom, right, and left. By pinning it to all four sides, it will stretch and shrink as the form resizes.
Use these in conjunction with the MinimumSize and MaximumSize properties to keep the image from getting too small or too large.
Upvotes: 4