问题
How to resize the Picturebox so it can show the full image if the image size is less than monitor size ! I wrote a code which can not resize (but still posting the code)!
Code before loading image on a button click
Dim bmp As Bitmap
bmp = New Bitmap(path)
If bmp.Width < picBox.Image.Width Then picBox.Width = bmp.Width : If bmp.Height < picBox.Image.Height Then picBox.Height = bmp.Height
picBox.Invalidate() : picBox.Refresh()
'picBox.SetBounds(x,y,width,height)
The code does not resize the picturebox, it's just untouched !
Edit
The form has the picBox and a groupbox [dock enabled] control only.
bmp = New Bitmap(dlgOpen.FileName)
picBox.SizeMode = PictureBoxSizeMode.Normal
Dim w As Integer = picBox.ClientSize.Width
Dim h As Integer = picBox.ClientSize.Height
If bmp.Width > w Then
w = bmp.Width
End If
If bmp.Height > h Then
h = bmp.Height
End If
If w > Me.Width - grpBox.Width Then
w = Me.Width - grpBox.Width
End If
If h > grpBox.Height Then
h = grpBox.Height
End If
picBox.ClientSize = New Size(w, h)
picBox.ImageLocation = dlgOpen.FileName
This code does not re-sizes the picture box either .
回答1:
In .NET, there's the Public Enumeration PictureBoxSizeMode
that allows you to change how the PictureBox handles differently sized images:
- Normal
- StretchImage
- AutoSize
- CenterImage
- Zoom
You can set it for the current PictureBox via the .SizeMode
property. AutoSize
is probably what you are looking for. If it is larger than the window or frame, you will have to handle this in a PictureBox.Resize
event to either resize the window or rescale the image.
So, it might be something like:
Dim bmp As Bitmap
bmp = New Bitmap(path)
picBox.SizeMode = PictureBoxSizeMode.AutoResize
picBox.Image = bmp
回答2:
Dim bmp As New Bitmap(path)
PictureBox1.SizeMode = PictureBoxSizeMode.Normal
Dim w As Integer = PictureBox1.ClientSize.Width
Dim h As Integer = PictureBox1.ClientSize.Height
If bmp.Width > w Then
w = bmp.Width
End If
If bmp.Height > h Then
h = bmp.Height
End If
If w > maxWidth Then
w = maxWidth
End If
If h > maxHeight Then
h = maxHeight
End If
PictureBox1.ClientSize = New Size(w, h)
PictureBox1.Image = bmp
回答3:
Picture-box can be re-sized only before loading the image, there after it is read only and has no effect !
来源:https://stackoverflow.com/questions/11724995/resize-picturebox-to-match-image-size