If File.Exists($"IdBG\Member Picture\{rw.Item("Code")}.jpg") Then
Dim ProPic As Image = CorectRotate(Image.FromFile($"IdBG\Member Picture\{rw.Item("Code")}.jpg"))
Dim x, y, rt As Double
If ProPic.Width < ProPic.Height Then
rt = 381 / ProPic.Width
y = ((ProPic.Width * rt) - (456 * rt)) * (ProPic.Width / 381) / 2
Else
rt = 456 / ProPic.Height
x = ((ProPic.Height * rt) - (381 * rt)) * (ProPic.Height / 456) / 2
End If
tmpIm = New Bitmap(ProPic, ProPic.Width, ProPic.Height)
grp.DrawImage(tmpIm, New Rectangle(61, 319, 381, 456), New Rectangle(x, y, ProPic.Width - (x * 2), ProPic.Height - (y * 2)), GraphicsUnit.Pixel)
End If
ทำได้แล้วครับ หลังจากศึกษาการหา ratio แล้วทำฟังค์ชั่นลองผิดลองถูก แล้วก็ทำได้อย่างงงๆ Code (VB.NET)
Function GetCropRectangle(xRatio As Integer, yRatio As Integer, w As Integer, h As Integer) As Rectangle
Dim xCor As Integer = (h / yRatio) / (h / xRatio) * h
Dim yCor As Integer = (w / xRatio) / (w / yRatio) * w
Dim CropLeft As Boolean
If (xCor > w) And (yCor < h) Then
CropLeft = False
ElseIf (xCor < w) And (yCor > h) Then
CropLeft = True
End If
If CropLeft Then
Return New Rectangle((w - xCor) / 2, 0, w - (w - xCor), h)
Else
Return New Rectangle(0, (h - yCor) / 2, w, h - (h - yCor))
End If
End Function