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
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetPictureBoxImage(PictureBox1, "C:\EDD-158.jpg")
End Sub
沒有留言:
張貼留言