Results 1 to 2 of 2

Thread: [RESOLVED] Scale a Size proportionally to fit within another Size

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Resolved [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.

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662

    Re: Scale a Size proportionally to fit within another Size

    VB Code:
    1. Private Shared Function ScaleSize(ByVal OriginalSize As Size, ByVal ToFit As Size) As Size
    2.         Dim ToFitRatio As Decimal = ToFit.Width / ToFit.Height
    3.         Dim OriginalRatio As Decimal = OriginalSize.Width / OriginalSize.Height
    4.  
    5.         Dim ret As Size
    6.  
    7.         If ToFitRatio < OriginalRatio Then
    8.             ret.Width = ToFit.Width
    9.             ret.Height = (ToFit.Height / OriginalRatio) * ToFitRatio
    10.         ElseIf ToFitRatio > OriginalRatio Then
    11.             ret.Width = (ToFit.Width * OriginalRatio) / ToFitRatio
    12.             ret.Height = ToFit.Height
    13.         Else
    14.             ret = ToFit
    15.         End If
    16.  
    17.         Return ret
    18.     End Function
    Last edited by agent; Feb 4th, 2007 at 06:37 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width