views:

533

answers:

2

Hello!

I use a fileReference.browse() to select an image file from the harddrive.

How can I check the Width and Height of the selected image file please?

Thank you!

+1  A: 

From the context of Flex, I'm pretty sure that onc you get the results back from a browserr, it is only a byteArray. In theory if you use that byteArray as the source for an image tag you'll be able to get the height and width that way, once you add that image to a container.

Otherwise, I do not believe there is an easy way to get such metadata info from local files using Flex.

www.Flextras.com
It's a ByteArray, yes. I thought about creating an Image object programatically, but was worried about the overhead of doing it that way.You are saying that even if I do that I have to use addChild to be able to measure it's content's dimmensions? I don't care about the container's size, just the Width and Height of the selected picture.Thanks!
Francisc
Yes, there may be overhead creating an image object. I do believe that an Image will not be measured until it is added to it's parent with the AddChild method, so height and width will be zero unless you set them explicitly.
www.Flextras.com
+2  A: 

Load the fileReference.data into a Loader using loadBytes(). Then you'll have: sourceBMP:Bitmap = loader.content as Bitmap;

Here is a sample code:

MXML part:

<fx:Declarations>
    <net:FileReference id="fileReference"
        select="fileReference_select(event);"
        complete="fileReference_complete(event);" />
</fx:Declarations>
<s:Button id="uplaodImageBtn"
    label="Upload Image"
    click="uplaodImageBtn_clickHandler()"/>

AS3 part:

private function uplaodImageBtn_clickHandler() : void {
    var arr:Array = [];
    arr.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
    fileReference.browse(arr);
}

private function fileReference_select(evt:Event):void {
    fileReference.load();
}

private function fileReference_complete(event:Event):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
    loader.loadBytes(fileReference.data);
}

public function loader_complete (event:Event) : void {
    var sourceBMP:Bitmap = event.currentTarget.loader.content as Bitmap;
    Alert.show(sourceBMP.width + ', ' +sourceBMP.height);
}
andi
Thank you, Andi!I wonder how much overhead such a technique would add to an application though.But excellent answer. Thanks!
Francisc