The resulting image has to be a rectangle, of course, but you can mask with transparency using BitmapData.draw
and BlendMode
:
var originalImage:BitmapData; // defined
var maskPath:GraphicsPath; // defined
var maskShape:Shape = new Shape();
maskShape.graphics.beginFill(0, 0); // fill region with transparent
maskShape.graphics.drawRect(0, 0, originalImage.width, originalImage.height);
maskShape.graphics.endFill();
maskShape.graphics.beginFill(0xFF0000);
maskShape.graphics.drawPath(maskPath.commands, maskPath.data, maskPath.winding);
maskShape.graphics.endFill();
var resultImage:BitmapData = originalImage.clone();
resultImage.draw(maskShape, null, null, BlendMode.ALPHA);
For cropping, you would probably do something more fancy in the last few lines--copying a region instead of cloning the whole originalImage
, and/or applying a transform when applying the maskShape
.
(I believe it's necessary to use a DisplayObject
to use BlendMode
s, but that's not clear in the documentation.)