画像を指定の範囲に収まるように縮小するメソッド

またどっかで使いそうな気がするので。

Public Shared Sub SetImageSize(imagePath As String, ByRef ptTop As Decimal, ByRef ptLeft As Decimal, ByRef ptHeight As Decimal, ByRef ptWidth As Decimal)
    '画像のサイズと解像度を取得する
    Dim pxImageHeight As Long = 0
    Dim pxImageWidth As Long = 0
    Using image As New System.Drawing.Bitmap(imagePath)
        pxImageHeight = image.Height
        pxImageWidth = image.Width
    End Using
    '比率を維持したまま表示
    Dim ptMaxHeight As Long = ptHeight
    Dim ptMaxWidth As Long = ptWidth
    If pxImageWidth <> 0 Then
        If pxImageWidth / pxImageHeight > ptWidth / ptHeight Then
            '幅を基準に縮小
            ptHeight = pxImageHeight * (ptMaxWidth / pxImageWidth)
            ptWidth = ptMaxWidth
        Else
            '高さを基準に縮小
            ptHeight = ptMaxHeight
            ptWidth = pxImageWidth * ptMaxHeight / pxImageHeight)
        End If
    Else
        ptHeight = ptMaxHeight
        ptWidth = ptMaxWidth
    End If

    '画像の位置の計算
    Dim ptImageY As Long = ptTop
    Dim ptImageX As Long = ptLeft
    '中心位置
    Dim ptCenterY As Long = ptImageY + (ptMaxHeight / 2)
    Dim ptCenterX As Long = ptImageX + (ptMaxWidth / 2)
    '上位置計算
    ptTop = ptCenterY - (ptHeight / 2)
    '横位置計算
    ptLeft = ptCenterX - (ptWidth / 2)
End Sub