This changes the situation, but not dramatically. Check out the modified solution:
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;
public class Test extends Sprite {
private var _fileReference:FileReference;
private var _fileFilter:FileFilter;
private var _imageLoader:Loader;
private var _maskLoader:Loader;
private var _canvas:Sprite;
private var _imageLoaded:Boolean = false;
public function Test() {
addEventListener(Event.ADDED_TO_STAGE, addedToStageListener, false, 0, true);
}
private function addedToStageListener(event:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, addedToStageListener);
_fileReference = new FileReference();
_fileReference.addEventListener(Event.SELECT, fileSelectedListener, false, 0, true);
_fileReference.addEventListener(Event.COMPLETE, fileLoadCompleteListener, false, 0, true);
_fileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");
_imageLoader = new Loader();
_imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadCompleteListener, false, 0, true);
_maskLoader = new Loader();
_maskLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, maskLoadCompleteListener, false, 0, true);
_canvas = new Sprite();
addChild(_canvas);
stage.addEventListener(MouseEvent.CLICK, mouseClickListener, false ,0, true);
}
private function mouseClickListener(event:Event):void {
_fileReference.browse([_fileFilter]);
}
private function fileSelectedListener(event:Event):void {
_fileReference.load();
}
private function fileLoadCompleteListener(event:Event):void {
if (!_imageLoaded) {
_imageLoader.loadBytes(event.target.data);
} else {
_maskLoader.loadBytes(event.target.data);
}
}
private function imageLoadCompleteListener(event:Event):void {
_imageLoaded = true;
}
private function maskLoadCompleteListener(event:Event):void {
var imageBitmap:Bitmap = _imageLoader.content as Bitmap;
var maskBitmap:Bitmap = _maskLoader.content as Bitmap;
if (imageBitmap && maskBitmap) {
var imageBitmapData:BitmapData = imageBitmap.bitmapData;
var maskBitmapData:BitmapData = maskBitmap.bitmapData;
var boundRectange:Rectangle = new Rectangle(0, 0, maskBitmap.width, maskBitmap.height);
var destinationPoint:Point = new Point(0, 0);
var finalBitmapData:BitmapData = new BitmapData(maskBitmap.width, maskBitmap.height);
finalBitmapData.copyPixels(imageBitmapData, boundRectange, destinationPoint, maskBitmapData, destinationPoint);
var finalBitmap:Bitmap = new Bitmap(finalBitmapData);
while(_canvas.numChildren) {
_canvas.removeChildAt(0);
}
_canvas.addChild(finalBitmap);
}
}
}}
First click on the stage allows you to select masked image. Second click allows you to select image, which contains the masking information (e.g. png file with transparency, like your image). Hope this helps.