RSS

How to get a DisplayObject’s BitmapData

This entry was posted on Jun 17 2008

There are many uses to using a DispalyObject’s (MovieClip, Sprite, Loader, etc…) BitmapData. The main reason I find myself using it for is for scaling an image I loaded smoothly. I load the image from an external source, then pull it’s BitmapData and set it’s smoothing to true. Then I can scale the new image without it getting too grainy or pixelated. This is the same effect as selecting the “Allow Smoothing” checkbox if you are using a Bitmap from the library.

In this example, we will get the BitmapData from our MovieClip (myMC) and create a new Bitmap (b), then add it to the stage:
 

var bd:BitmapData = new BitmapData(myMC.width, myMC.height, true,0xFFFFFFFF);
bd.draw(myMC);
var b:Bitmap = new Bitmap(bd);
addChild(b);

I used the BitmapData.draw() method to pull the movie clip’s (myMC) pixel information. I can then use that data when creating a new Bitmap. All that is left is to add the new Bitmap (b) to the display tree (addChild).


2 Responses to “How to get a DisplayObject’s BitmapData”

  1. Thanks, I just want to know that as well.

    But there should be a reason right ? when we need this bitmap data out of the ability MovieClip, Sprite, etc do the job for us. For my case, I use your solution to load the external resource (it automatically loaded as DisplayObject) and get its BitmapData to inject into the Entity’s image of some flash 2d engine.

    THanks :)


  2. Found this useful for same technique haxpor mentions: Loading animated PNG sequences as DisplayObjects then injecting their bitmapData into Bitmaps on the stage. Thanks for saving me a headache!


Post a Comment