Code snippet to take minimal area from BitmapData where something is painted. For example user will paint something in your Flex application and you want to take minimal area and do object from this painting. (It’s code from bigger project, some refactoring is needed)
protected function makeObjectFromDrawing(bmpData:BitmapData):void
{
var bmpData1bit:BitmapData = new BitmapData(bmpData.width, bmpData.height, true, 0x00000000);
var original:Bitmap = new Bitmap(bmpData);
var c:UIMovieClip = new UIMovieClip();
c.mouseChildren = false;
var operation:String = "<";
var threshold:uint = 0xFFFFFF;
var color:uint = 0xFFFFFF00;
var mask:uint = 0xFFFF0000;
// create bitmap where all what was drawn is transparent and border is yellow
bmpData1bit.threshold(bmpData, bmpData.rect, new Point(0,0), operation, threshold, color, mask, false);
// find bound of transparent color
var rect:Rectangle = bmpData1bit.getColorBoundsRect( 0xFFFFFFFF, 0x00000000 );
if(rect.width == 0 || rect.height == 0) return;
var outline:Sprite = new Sprite();
outline.graphics.clear();
outline.graphics.lineStyle(1, 0xFF0000, 1);
outline.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
// add outline
// c.addChild(outline);
// ad there where was original
c.x = rect.x;
c.y = rect.y;
// create new tranparent bitmap data and draw selection
bmpData = new BitmapData(rect.width, rect.height, true, 0x00000000);
// correct position
var m:Matrix = new Matrix();
m.tx = -rect.x;
m.ty = -rect.y;
rect.x = rect.y = 0;
bmpData.draw(original, m, null, null, rect);
original = new Bitmap(bmpData);
c.addChild(original);
this.addElement(c);
}