Uploading an image dynamically into Flash
I'd upload an image choosen by the client, but I want to wrap it in a movieclip in order to manipulate its size and color with color transform. How can I do it? (AS3 in Flash 10)
My code:
import flash.display.SimpleButton;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.events.IOErrorEvent;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.display.*;
import flash.geom.*;
var loadFileRef:FileReference;
const FILE_TYPES:Array = [new FileFilter("Image Files", "*.jpg;*.jpeg;*.gif;*.png;*.JPG;*.JPEG;*.GIF;*.PNG")];
function loadFile(event:MouseEvent):void {
loadFileRef = new FileReference();
loadFileRef.addEventListener(Event.SELECT, onFileSelect);
loadFileRef.browse();
}
function onFileSelect(e:Event):void {
loadFileRef.addEventListener(Event.COMPLETE, onFileLoadComplete);
loadFileRef.load();
}
function onFileLoadComplete(e:Event):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onDataLoadComplete);
loader.loadBytes(loadFileRef.data);
loadFileRef = null;
}
function onDataLoadComplete(e:Event):void {
var bitmapData:BitmapData = Bitmap(e.target.content).bitmapData;
if((bitmapData.height)<=(bitmapData.width))
{
var r:Number = bitmapData.height/bitmapData.width;
var ra:Number = 500*r;
var matrix:Matrix = new Matrix();
matrix.scale(500/bitmapData.width, ra/bitmapData.height);
graphics.clear();
graphics.lineStyle(1, 0x000000);
graphics.beginBitmapFill(bitmapData, matrix, false);
graphics.drawRect(0, 0, 500, ra);
graphics.endFill();
} else {
var d:Number = bitmapData.width/bitmapData.height;
var da:Number = 500*d;
var matrix2:Matrix = new Matrix();
matrix2.scale(500/bitmapData.width, da/bitmapData.height);
graphics.clear();
graphics.lineStyle(1, 0x000000);
graphics.beginBitmapFill(bitmapData, matrix2, false);
graphics.drawRect(0,50, 500, da);
graphics.endFill();
}
}
boton.addEventListener(MouseEvent.CLICK, loadFile);
Any help will be very appreciated. Madrid.
