Monday, March 8, 2010

Getting Image Width at Runtime

Recently I ran with some requirements and needed to get the size of an uploaded image in Flex, which led me to getting an insight of the size data that the image stores.

So what we have and usually use is the width/height property of the Image control. This is a property that is explicitly set by the developer. If not set, the property takes the value of the image’s width and height. To find the final size one has to listen to the updateComplete event.

The other property of the image is the contentWidth/contentHeight. This property gives the size of the scaled image based on the scale ratios. Again this can be found if one listens to the updateComplete event.

The final property [the one that I was looking for :)], can be found inside the content property in the image. The image.content contains the unscaled width and height of the original image data. We can access it using the image.content.width/image.content.height property. What's good is that this property is updated on the complete event of the image load.

-- Cheers.

No comments :