views:

9

answers:

1
package com {
    import flash.display.*;
    import flash.net.*;
    import flash.events.*;

    public class Test extends Sprite {
        public var mc:MovieClip = new MovieClip();
        public var buttonShape:Shape = new Shape();
        public var fileRef:FileReference= new FileReference();

        public function Test() {
            buttonShape.graphics.beginFill(0x336699);
            buttonShape.graphics.drawCircle(50, 50, 25);
            var button = new SimpleButton(buttonShape, buttonShape, buttonShape, buttonShape);
            addChild(button);
            button.addEventListener(MouseEvent.CLICK, onButtonClick);

        }

        public function onButtonClick(e:MouseEvent):void {
            fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);
            fileRef.addEventListener(Event.SELECT, onFileSelected);
        }

        public function onFileSelected(e:Event):void {
            fileRef.addEventListener(Event.COMPLETE, onFileLoaded);
            fileRef.load();
        }

        public function onFileLoaded(e:Event):void {
            var loader:Loader = new Loader();
            loader.loadBytes(e.target.data);
            mc.addChild(loader);
            addChild(mc);
        }

    }
} 

The code above displays the user selected image. How to get the orginal width and height of the selected image and how set new width and height?

A: 
public function onFileLoaded(e:Event):void {
     var loader:Loader = new Loader();
     loader.contentLoaderInfo.addEventListener(Event.COMPLETE , completeHandler );
     loader.loadBytes(e.target.data);
     mc.addChild(loader);
     addChild(mc);
        }

private function completeHandler(event:Event):void
{
   var image:DisplayObject = event.currentTarget.loader.content;
   var _width:int = image.width;
   var _height:int = image.height;

    var newWidth:int = 100;
    var newHeight:int = 80;

   //here you can either scale image or directly set the new dimensions
   image.width = newWidth;
   image.height = newHeight;

}
PatrickS