开发者

Resizing user selected image

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 functio开发者_如何学Cn 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?


To maintain the aspect ratio of the selected image to load, I've modified the code above to:

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

// Scale the bitmap to fit the display area
            if (image.width > 500 || image.height > 400)
            {
                var hRatio:Number = image.width / 500;
                var vRatio:Number = image.height / 400;

                if (hRatio >= vRatio)
                {
                    image.width = 500;
                    image.scaleY = image.scaleX;
                }
                else
                {
                    image.height = 400;
                    image.scaleX = image.scaleY;
                }
            }

            // Center the bitmap in the display area
            image.x = (500 - image.width) / 2;
            image.y = (400 - image.height) / 2;     

}


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;

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜