Size form proportionally to screen size

sarthemaker

Active member
Joined
Sep 4, 2009
Messages
36
Programming Experience
5-10
If I have form say sized 1300 x 800, I would like to proportionally size the form to the computers screen size. I can get it to size to the screen size, but it is not proportional..

I have been using "screen.primaryscreen.bounds" for getting the size of the current screen.

Does anyone have any ideas on how I can accomplish this?
 
Doesn't setting the form windowstate to maximized fit your needs?

VB.NET:
    Private Sub Form1_Load(...) Handles MyBase.Load

        Me.WindowState = FormWindowState.Maximized

    End Sub

Or set the property right in the properties window of the form designer.
 
Actually, this won't work, because it stretches the form to the screen, I need it proportionally size the form to the screen. Like the zoom effect in a picturebox.
 
I tried your scaleToFit function, but it doesn't seem to be taking into account the height and width of the image, it is scaling the form to the proportions of the screen, if I have a square image that's bigger than the screen, it re-sizes the form as larger width than height (because I have a wide screen) even though the image is square. Here is my code:

VB.NET:
Dim r As Rectangle = ScaleToFit(New Rectangle(0, 0, img.Width + 4, img.Height + 53), New Rectangle(0, 0, Screen.PrimaryScreen.Bounds.Width - 104, Screen.PrimaryScreen.Bounds.Height - 153))
setWidth = r.Size.Width
setHeight = r.Size.Height

Am I doing something wrong here?
 
The code scales the form rectangle to fit inside the screen rectangle without changing the aspect ratio, that is what proportional means, and what you asked to do. This works no matter which side or rectangle is larger.

If you have a picturebox that resizes dynamically and want it to display the image without changing aspect ratio you have to set SizeMode to Zoom. I'm not sure if this is what you're talking about now.
 
Well, what I'm trying to do is display an image in a form, and the image fills the entire form, but if the image is larger than the screen, I need the form to scale to fit within the screen according to the proportions of the image being displayed. So the image can still be proportional but still fill the form.
 
Why not set form state to max and pic box dock and zoom, simple as that.
 
But what if I have an image that is 1450x500? There will be a lot of space above and below the image, I need the form to be the exact size of the image, and then shrink it to fit within the screen..
 
Well, the screen WorkingArea bounds the form, form ClientSize bounds picturebox, and picturebox clientsize bounds image display size. The difference between a controls size and ClientSize doesn't change with different sizes, so these can be deducted right away. Now you have available 'working size', image size is given. Place them into ScaleToFit function and you have the result used to set ClientSize of Picturebox, which in turn give the Size to set the forms ClientSize. Location of picturebox and form can be (0,0). These are the calculations:
VB.NET:
Dim target = Screen.PrimaryScreen.WorkingArea.Size - (Me.Size - Me.ClientSize) - (Me.PictureBox2.Size - Me.PictureBox2.ClientSize)
Dim source = Me.PictureBox2.Image.Size
Dim fit = Size.Round(ScaleToFit(source, target))
set control sizes:
VB.NET:
Me.PictureBox2.ClientSize = fit        
Me.ClientSize = Me.PictureBox2.Size
and perhaps their location:
VB.NET:
Me.PictureBox2.Location = Point.Empty
Me.Location = Point.Empty
Since the image usually is larger or smaller than the available working size you set SizeMode to Zoom.
 
That is not possible, unless there is more to the question that you are telling. ScaleToFit will always return a box that is the best fit of source box in target box, without changing aspect ratio of source box. The effect is exactly the same as the Zoom mode of picturebox.
 
Back
Top