Friend Sub SetPictureBoxImage(ByVal pb As PictureBox, ByVal sFileName As String)
'定义一个Bitmap对象作为绘制的接受对象
Dim bmp As New Bitmap(pb.Width, pb.Height)
Dim g As Graphics = Graphics.FromImage(bmp)
Dim img As Image = Image.FromFile(sFileName)
Dim rectImage As New Rectangle(0, 0, bmp.Width, bmp.Height)
'按比例缩放
GetScaleZoomRect(img.Width, img.Height, rectImage.Width, rectImage.Height)
g.DrawImage(img, rectImage)
pb.Image = bmp
End Sub
Friend Function GetScaleZoomRect(ByVal nSrcWidth As Integer, ByVal nSrcHeight As Integer, ByRef nDstWidth As Integer, ByRef nDstHeight As Integer)
If nSrcWidth / nSrcHeight < nDstWidth / nDstHeight Then
nDstWidth = nDstHeight * (nSrcWidth / nSrcHeight)
Else
nDstHeight = nDstWidth * (nSrcHeight / nSrcWidth)
End If
End Function