Flex 4 fileReference selected image file dimmensions (width and height)

Francisc picture Francisc · Aug 9, 2010 · Viewed 7.6k times · Source

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!

Answer

andi picture andi · Aug 29, 2010

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);
}