Jun
30
2008
AS3 | Flex | Programming
Scaling a DisplayObject to fit to a container’s dimensions
I’ve often run into the problem of proportionally scaling an image (or other DisplayObject) to the dimensions of its parent container. I always end up having to scavenge around in my code bank for the line of code to do this—which really is a simple bit of code—but I prefer to copy-and-paste it than rewrite it. SO I am posting it here so I never have to search for it again:
var scale:Number = ((container.height/container.width) > (image.height/image.width)) ? (container.width/image.width) : (container.height/image.height); image.scaleX = scale; image.scaleY = scale;

August 12th, 2008 at 9:38 am
Dude, you just saved me a days work. This shit always frys my brain and frustrates me because I know there’s always a simple solution … and there it is. Thanks.
June 16th, 2009 at 8:25 pm
Here is the equivalent structure in VBScript:
Save the dimensions of the image in a database when it is uploaded to the site using an upload script or something. Set the dimensions of your container as a variable, then you can turn this into a function and call it whenever you need it.
If Container_H / Container_W > Img_H / Img_W Then
Img_Resize_Percentage = Container_W / Img_W
Else
Img_Resize_Percentage = Container_H / Img_H
End If
‘ resize the image
‘ set image width
New_Width = round(Img_W * Img_Resize_Percentage)
‘ set Image Height
New_Height = round(Img_H * Img_Resize_Percentage)