[RESOLVED] Scale a Size proportionally to fit within another Size
My app will be doing some proportional image scaling. To figure the size to scale to, I have a function that takes the original size and the target size and scales it proportionally to fit within the target size (i.e. without stretching in, in other words, zoom to fit).
It's a little embarrassing, but I can't seem to work out the math in my head for this. I'm not worried about the position of the image as it is always centered.
Can anybody help?
Code:
Private Shared Function ScaleSize(ByVal OriginalSize As Size, ByVal Target As Size) As Size
End Function
Yes. Honestly, all I have for this is an empty function. Again, I'm slightly embarrassed at this.
Re: Scale a Size proportionally to fit within another Size
VB Code:
Private Shared Function ScaleSize(ByVal OriginalSize As Size, ByVal ToFit As Size) As Size
Dim ToFitRatio As Decimal = ToFit.Width / ToFit.Height
Dim OriginalRatio As Decimal = OriginalSize.Width / OriginalSize.Height
Dim ret As Size
If ToFitRatio < OriginalRatio Then
ret.Width = ToFit.Width
ret.Height = (ToFit.Height / OriginalRatio) * ToFitRatio
ElseIf ToFitRatio > OriginalRatio Then
ret.Width = (ToFit.Width * OriginalRatio) / ToFitRatio
ret.Height = ToFit.Height
Else
ret = ToFit
End If
Return ret
End Function