Results 1 to 3 of 3

Thread: [RESOLVED] Size Constraints ASAP!

  1. #1

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Resolved [RESOLVED] Size Constraints ASAP!

    I have images that I want to display on a web page. I have user defined width and height limitations. If the original width of the image is > the user defined width or the original height of the image is > the user defined height then I need to shrink the image. The problem is that I need to shrink the image and maintain the aspect ratio. What is the formula to "fit" an image into a designated space while keeping its aspect ratio.

    For example if MaxWidth = 10 and MaxHeight = 5 and the image is 12 x 6, I would want the image to be 10 x 5.

    For example if MaxWidth = 10 and MaxHeight = 5 and the image is 6 x 12, I would want the image to be 2.5 x 5.

    Keep in mind there are cases where the one dimension may fit, but then the other dimension is still too large which would require the image to be made smaller.

    PLEASE HELP!!

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Size Constraints ASAP!

    Think about what you are doing. How do you know which one you want to be the defining dimension? What you would do is scale the width in order to match the height available. If it is smaller than the allowed width then you have the answer. If it is greater than the allowed width, then you need to scale the other way.

    VB Code:
    1. NewWidth = OldWidth x NewAllowedHeight/OldHeight
    2.  
    3.  If NewWidth <= NewAllowedWidth Then
    4.   NewWidth = NewWidth
    5.   NewHeight = NewAllowedHeight
    6.  Else
    7.   NewHeight = OldHeight x NewAllowedWidth/OldWidth
    8.   NewWidth = NewAllowedWidth
    9.  End If

    That's it.

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3

    Thread Starter
    Fanatic Member clarkgriswald's Avatar
    Join Date
    Feb 2000
    Location
    USA
    Posts
    799

    Re: Size Constraints ASAP!

    Yeah I had an algorithm going, but I was off by just a bit. Here's what I am going with:

    VB Code:
    1. Dim dOriginalRatio As Double
    2.     Dim dDestinationRatio As Double
    3.  
    4.     'get the original ratio
    5.     dOriginalRatio = lOriginalWidth / lOriginalHeight
    6.    
    7.     'get the destination ratio
    8.     dDestinationRatio = lNewWidth / lNewHeight
    9.    
    10.     'calculate destination width
    11.     lDestinationWidth = IIf(dDestinationRatio < dOriginalRatio, lNewWidth, lNewHeight * dOriginalRatio)
    12.    
    13.     'calculate destination height
    14.     lDestinationHeight = IIf(dDestinationRatio < dOriginalRatio, lNewWidth / dOriginalRatio, lNewHeight)

    Thanks for your reply!

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