Actionscript – How to load a bitmap file into a BitmapData object

actionscriptbitmapbitmapdatadrawingflash

In Flash, the BitmapData object can be used to store bitmaps in RAM, you can later draw them to a MovieClip using the beginBitmapFill() method.

How do you load an external bitmap file (.jpg) into a BitmapData object?

Even AS3 code would be helpful.

Best Answer

AS3 code to load a PNG and "get" its bitmapData

var bitmapData:BitmapData;

var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("../lib/img.png"));

function onComplete (event:Event):void
{
    bitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
}