Resize PictureBox to match image size

女生的网名这么多〃 提交于 2019-12-13 04:08:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!