RSS

AS3 Capture using BitmapData

Actionscript 3 | Posted on Jun 14 2012

A function help to grab part of Object using BitmapData:

function cropBitmap( ARG_object:DisplayObject, ARG_x:Number, ARG_y:Number, ARG_width:Number, ARG_height:Number):Bitmap {
    // create a rectangle of the specific crop size
    var cropArea:Rectangle = new Rectangle(0, 0, ARG_width, ARG_height);
    // create a BitmapData object the size of the crop
    var bmpd:BitmapData = new BitmapData(ARG_width, ARG_height);
    // create the cropped Bitmap object from the bitmap data
    var croppedBitmap:Bitmap = new Bitmap(bmpd, PixelSnapping.ALWAYS, true);
    // create the matrix that will shift the crop from 0,0
    var cropMatrix:Matrix = new Matrix();
    cropMatrix.translate(-ARG_x, -ARG_y);
    // draw the supplied object, cropping to the cropArea with the cropMatrix offseting the result
    bmpd.draw( ARG_object, cropMatrix, null, null, cropArea, true );
    return croppedBitmap; // return the cropped bitmap
}