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);
}
}
}
上面的代码显示用户选择的图像。如何获得所选图像的原始宽度和高度以及如何设置新的宽度和高度?
答案 0 :(得分:1)
要保持所选图像的宽高比加载,我已将上面的代码修改为:
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;
}
答案 1 :(得分:0)
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; }