 |
|
จะ resize แบบ keep aspect ratio รูปยังไงถ้า ความกว้างกับความสูงเท่ากันครับ |
|
 |
|
|
 |
 |
|
Code (VBScript)
จะ resize แบบ keep aspect ratio รูปยังไงถ้า ความกว้างกับความสูงเท่ากันครับ
กรอบรูปผมใช้ ratio 381:456 (5:6) ผม resize ให้รูปมันฟิตกับเฟรมพอดี แล้วตัดส่วนเกินออก (crop)
แต่พอความกว้างความสูงเท่ากัน รูปมันยืดผิดปกติ
ค้นหาวิธีแก้ไปเจอในเว็บนี้ปัญหาเดียวดัน แต่ผมอ่านไม่ออก เขียน C# ไม่เป็น: https://stackoverflow.com/questions/1940581/c-sharp-image-resizing-to-different-size-while-preserving-aspect-ratio
นี่เป็นโค้ดผมครับ
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
Tag : .NET, VB.NET
|
|
 |
 |
 |
 |
Date :
2018-02-16 22:47:04 |
By :
nk4ever |
View :
1073 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำได้แล้วครับ หลังจากศึกษาการหา 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
|
ประวัติการแก้ไข 2018-02-17 12:46:42
 |
 |
 |
 |
Date :
2018-02-17 12:36:50 |
By :
nk4ever |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|