ActionScript 3 Drawing Bitmaps Draw a display object into bitmap data


A helper function to create a bitmap copy of an object. This can be used to convert vector objects, text or complex nested Sprite's to a flattened bitmap.

function makeBitmapCopy(displayObj:IBitmapDrawable, transparent:Boolean = false, bgColor:uint = 0x00000000, smooth:Boolean = true):Bitmap {

    //create an empty bitmap data that matches the width and height of the object you wish to draw
    var bmd:BitmapData = new BitmapData(displayObj.width, displayObj.height, transparent, bgColor);
    //draw the object to the bitmap data
    bmd.draw(displayObj, null, null, null, null, smooth);
    //assign that bitmap data to a bitmap object
    var bmp:Bitmap = new Bitmap(bmd, "auto", smooth);
    return bmp;


var txt:TextField = new TextField();
txt.text = "Hello There";

var bitmap:Bitmap = makeBitmapCopy(txt, true); //second param true to keep transparency