Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
Hi dears
I want to keep my picture box size while I resize my form, but as soon as I re-size the form the picture box' width-length ratio changes, which I don't want it to.
Posted
Updated 14-Nov-13 22:12pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Nov-13 3:48am    
This is not about a picture box, but the image in it. Not quite clear what do you want to achieve.
—SA
Marco Bertschi 15-Nov-13 4:08am    
It is about the picture box, at least IMO - He wants to keep the size of the picture box when he re-sizes the window, which is a clear indicator that there is something wrong with the picture box' anchor settings.
Sergey Alexandrovich Kryukov 15-Nov-13 10:02am    
It does not have to be anchors at all. I guess, size is changed, but aspect ratio should be kept. A trivial problem...
—SA

Here's one way you could approach this: create an EventHandler for the PictureBox SizeChanged Event:
C#
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
    if (pictureBox1.Width == pictureBox1.Height) return;

    if (pictureBox1.Width > pictureBox1.Height)
    {
        pictureBox1.Height = pictureBox1.Width;
    }
    else
    {
        pictureBox1.Width = pictureBox1.Height;
    }
}
The code shown here will adjust either the Width or Height of the PictureBox to be equal to the larger dimension.

If your PictureBox is anchored in a way that only the Width can change, then you can simplify the above code by only testing for Width > Height, and adjusting the Height.

You could also constrain the PictureBox size by setting its MinimumSize and MaximumSize properties. Or, you could, in the SizeChanged EventHandler, constrain the size.

You also should consider the possible effect of the PictureBox's Location when it's resized: depending on the Location and the scaling done, the PictureBox could become partially outside your Form, or other ContainerControl's, bounds.

Note that when you launch a WinForms program, the SizeChanged EventHandler is going to be called twice by the program starting up (tell Microsoft if you don't like this). Often I set a boolean flag in a Form Load EventHandler, and then, in Controls like the PictureBox I test for that boolean flag and exit the EventHandler if it's true. I usually reset that boolean flag to false at the end of the Form Load EventHandler, less often in the Form Shown EventHandler.
 
Share this answer
 
v3
Comments
alireza ghasemi 15-Nov-13 6:24am    
Thank you very much dear BillWoodruff.
Sergey Alexandrovich Kryukov 15-Nov-13 10:02am    
Sure, a 5.
—SA
What I think is happening that you have activated the bottom and right anchor of the picture box.
I think that you should try to only activate the Anchors to top and left of the picture box.

cheers,
Marco
 
Share this answer
 
Comments
alireza ghasemi 15-Nov-13 4:13am    
Hi dear
you understood me very well.
But I have to keep the picture size as maximum as possible. so I have to Anchor my pic box both right and left to stretch my pic box but I want to it's height stretch too.
Marco Bertschi 15-Nov-13 4:24am    
So the picture box is filling the whole form, in the end?

Can you upload screenshots of your form, somewhere?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900