Click here to Skip to main content
15,069,297 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to create this.

I have a form3 added this :
- Panel1 - Dock - Fill
- Picturebox1 - Inside Panel
- Picturebox1 - AutoSize

What i want , when form opens to load the image and resize it to show the full image view.
Because i have some images that are small,others are big and i want the picturebox to correct display the picture size (not to make the form so big that the picture to be in center or stuff like this. Correctly the size of form to be as the picture size)

How to do that?

What I have tried:

Private Sub Form3_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        Me.Size = New Size(Me.Width + Me.PictureBox1.Width - Me.Panel1.Width,
                        Me.Height + Me.PictureBox1.Height - Me.Panel1.Height)
    End Sub
Updated 8-Jun-20 8:19am

Changing the size inside a Resize event is a bad idea - it can easily lead to an infinite loop, as changing the Size property is what triggers a Resize event ...

And I have to say, changing the form size to fit the picture is an odd way to go about things: what if the picture turns out to be 1x1 pixel? 10K x 10K pixels? I've used a lot of graphics packages for example and not one of them has tried your way, for good reasons.

The way I would do it is to change the form so it can't be resized, and set the form size when the picture is loaded - checking then for "sensible" height and width values.
Well, no - I probably wouldn't - I'd let the user control how big the form and picture are as it's his monitor(s) that it has to fit on ... I for example have three, and I'd want it to fit on all three: the Portrait, the Landscape, and the Square which would need different sizes depending on which monitor I displayed it on.
diablo22 8-Jun-20 13:14pm
you didn't understand me correct i will not load pictures that are 1000x1000 or more.
The point of this is to load form and show the picture itself nothing else. Some pictures are small others are little more like little more is (200x150) for example.
Thats why i want the form to be resized as the picture size it is. Its like Preview image form display
1. Panel1 - Dock - Fill
2. Panel1 - AutoSizeMode - GrowAndShrink
3. Picturebox1 - Location - 0;0
4. Picturebox1 - AutoSize
5. Form3 - AutoSizeMode - GrownOnly
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 Me.Size = New Size(Me.Width + Me.PictureBox1.Width - Me.Panel1.Width,
                       Me.Height + Me.PictureBox1.Height - Me.Panel1.Height)
End Sub

I have amazed myself again, before someone even knows the answer Gj to me :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

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