Question

I am trying to make a .Net WinForms window with a simple Toolstrip and a single picture box.

   Dim img As Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filename)))
   PictureBox1.Image = img
   PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
   ratio = img.Height / img.Width

The image displays fine, my problem is that I'm trying to get the Form to size correctly to display the image at its correct resolution with its correct aspect ratio. And because there is more to the form that just the picturebox I'm not sure how to the resize.

If I do

Me.Width = Me.Height * Ratio 

then the ratio isn't accurate due to the space required for the window borders and Toolstrip. How do I set the Forms initial size to make the Picturebox size exactly equal to the image dimensions?

Found the answer is:

Me.ClientSize = New System.Drawing.Size(img.Width, img.Height + ToolStrip1.Height)

To resize while maintaining the aspect ratio the code is:

Dim ratio as Double

Private Sub Form_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
  Dim img As Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(_filename)))
  PictureBox1.Image = img
  PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
  Image_ratio = img.Height / img.Width

  Me.ClientSize = New System.Drawing.Size(img.Width, img.Height + ToolStrip1.Height)
  Me.Activate()

  AddHandler Me.Resize, AddressOf Form_SizeChanged
End Sub

Private Sub Form_SizeChanged(sender As Object, e As EventArgs)
  'Resize while maintaining aspect ratio
   Me.ClientSize = New System.Drawing.Size(PictureBox1.Height / Image_ratio, Me.ClientSize.Height)
End Sub
Was it helpful?

Solution

Found the answer is:

Me.ClientSize = New System.Drawing.Size(img.Width, img.Height + ToolStrip1.Height)

To resize while maintaining the aspect ratio the code is:

Dim ratio as Double

Private Sub Form_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
  Dim img As Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(_filename)))
  PictureBox1.Image = img
  PictureBox1.SizeMode = PictureBoxSizeMode.Zoom
  Image_ratio = img.Height / img.Width

  Me.ClientSize = New System.Drawing.Size(img.Width, img.Height + ToolStrip1.Height)
  Me.Activate()

  AddHandler Me.Resize, AddressOf Form_SizeChanged
End Sub

Private Sub Form_SizeChanged(sender As Object, e As EventArgs)
  'Resize while maintaining aspect ratio
   Me.ClientSize = New System.Drawing.Size(PictureBox1.Height / Image_ratio, Me.ClientSize.Height)
End Sub

OTHER TIPS

Add a Panel to your form and set its Dock property to Fill, so that it fills all the space in the form other than that occupied by the ToolStrip. Add the PictureBox to the Panel, setting its Location to (0,0) and its SizeMode property to AutoSize. When you set the Image of the PictureBox, the Size will change automatically to fit the Image. You can then resize the form like so:

Private Sub PictureBox1_SizeChanged(sender As Object, e As EventArgs) Handles PictureBox1.SizeChanged
    Me.Size = New Size(Me.Width + Me.PictureBox1.Width - Me.Panel1.Width,
                       Me.Height + Me.PictureBox1.Height - Me.Panel1.Height)
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top