Results 1 to 5 of 5

Thread: Logic check and/or better way?

  1. #1

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Logic check and/or better way?

    Hate to be paranoid, but....

    I wrote a function that takes the height and width of a full size, uploaded image and returns - allegedly, It seems to anyway - new dimensions with the aspect ratio preserved.

    My function seems awkward. Could anyone suggest a better approach?
    Thanks!

    VB Code:
    1. Function RetainAspectRatio(ByVal imgHeight As Integer, ByVal imgWidth As Integer, ByVal IsItASmallThumb As Boolean) As ArrayList
    2.         Dim blnSizeCheck As Boolean = IsItASmallThumb
    3.         Dim maxheight As Integer
    4.         Dim maxwidth As Integer
    5.         If blnSizeCheck Then
    6.             maxheight = 93
    7.             maxwidth = 125
    8.         Else
    9.             maxheight = 300
    10.             maxwidth = 400
    11.         End If
    12.         imgHeight = Convert.ToDouble(imgHeight)
    13.         imgWidth = Convert.ToDouble(imgWidth)
    14.         'Get the new height and width depending on whether the end result will be a small thumb or medium-size image
    15.         'image
    16.         If (imgWidth > imgHeight) Or (imgWidth = imgHeight) Then
    17.             While imgWidth > maxwidth
    18.                 imgWidth = Math.Round(imgWidth - (imgWidth * 0.01), 0)
    19.                 imgHeight = Math.Round(imgHeight - (imgHeight * 0.01), 0)
    20.             End While
    21.         ElseIf imgHeight > imgWidth Then
    22.             While (imgHeight > maxheight) Or (imgWidth > maxwidth)
    23.                 imgWidth = Math.Round(imgWidth - (imgWidth * 0.01), 0)
    24.                 imgHeight = Math.Round(imgHeight - (imgHeight * 0.01), 0)
    25.             End While
    26.         End If
    27.         'send the new dimensions back in an arraylist
    28.         imgWidth = Convert.ToInt32(imgWidth)
    29.         imgHeight = Convert.ToInt32(imgHeight)
    30.         Dim arDimensions As New ArrayList
    31.         arDimensions.Add(imgWidth)
    32.         arDimensions.Add(imgHeight)
    33.         Return arDimensions
    34.     End Function

  2. #2
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Logic check and/or better way?

    Well I don't see a reason to dim blnSizeCheck, just use IsItASmallThumb.
    Also your converting the height and width to a double and the sticking it back into an integer which converts it straight back to an integer again...
    Is there a reason your using an arraylist? You could just use a string array like this;
    Code:
    Dim arDimensions(1) As string = {imgWidth, imgHeight}
    TPM

    Add yourself to the VBForums Frappr Map!!

  3. #3
    Fanatic Member -TPM-'s Avatar
    Join Date
    Jul 2005
    Posts
    850

    Re: Logic check and/or better way?

    I just noticed you also rounding when you subtract 1% which would make using a double pointless anyway. Looking at your if's and while's again I also realized you only really need the second one 'While (imgHeight > maxheight) Or (imgWidth > maxwidth)' as both need to resolve too true before it'll exit the loop and thus will keep scaling down untill both are withing the size limit
    TPM

    Add yourself to the VBForums Frappr Map!!

  4. #4
    Hyperactive Member tailz's Avatar
    Join Date
    Jul 2002
    Posts
    306

    Re: Logic check and/or better way?

    ug that is not the way to do it (if I get what you are trying to do)

    I dont have time to test this code but you need to work out the second dimension (depending on if height>width or width>height) based on a scale factor

    Code:
      dim scale as double 
       If (imgWidth >= imgHeight) Then
          'need to calculate the percent of the width to maxwidth
          scale= maxwidth/imgwidth
        Else  ' If imgHeight > imgWidth Then (dont need the logic check)
            scale= maxheight/imgHeight
       end if
       imgWidth = imgwidth * scale '(or imgwidth *= scale)
       imgHeight = imgheight * scale
    Summat like that anyway.

    also this line:
    If (imgWidth > imgHeight) Or (imgWidth = imgHeight) Then
    is the same as:
    If (imgWidth >= imgHeight) Then


    Hope that helps

    GaZ

  5. #5

    Thread Starter
    Hyperactive Member Rocketdawg's Avatar
    Join Date
    Feb 2003
    Location
    Back in the doghouse
    Posts
    294

    Re: Logic check and/or better way?

    Guys

    Sorry - been out, but thank you for your replies. I sniffed around, and ended up with this

    VB Code:
    1. If imgWidth > maxwidth Or imgHeight > maxheight Then
    2.             If (maxheight / imgHeight) > (maxwidth / imgWidth) Then
    3.                 scale = maxheight / imgHeight
    4.             Else
    5.                 scale = maxwidth / imgWidth
    6.             End If
    7.             If imgWidth > maxwidth Then
    8.                 scale = maxwidth / imgWidth
    9.                 imgWidth *= scale
    10.                 imgHeight *= scale
    11.             End If
    12.             If imgHeight > maxheight Then
    13.                 imgWidth /= scale
    14.                 imgHeight /= scale
    15.                 scalefactor = maxheight / imgHeight
    16.                 imgWidth *= scale
    17.                 imgHeight *= scale
    18.             End If
    19.         End If

    TPM - right, dumped the arraylist. Kept the boolean. This generates dimensions for one of 2 sizes of saved images, depending on the T or F.

    Thanks again

    Dawg

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