Monday, August 9, 2010

Working with Bitmaps in Actionscript 3: Part 2

Today we will learn how to load external images into a Bitmap object.

External bitmap image can be loaded using Loader class. You need to specify the URL of image. Complete event tells us when the loading ends. We create a loader, load an image and when it's loaded, pass it to the bitmap.

var myLoader:Loader = new Loader();

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
myLoader.load(new URLRequest("myimage.jpg"));

function loadComplete(event:Event):void {
var image:Bitmap = Bitmap(myLoader.content)
var bitmap:BitmapData = image.bitmapData;

We also defined a BitmapData object, so we can manipulate it. In the next example, we will draw a blue rectangle over the image:

function loadComplete(event:Event):void {
var loadedimage:Bitmap = Bitmap(myLoader.content)
var bitmap:BitmapData = new BitmapData(loadedImage.width, loadedImage.height, false, 0xffffffff);
bitmap.draw(loadedImage, new Matrix())
var image:Bitmap = new Bitmap(bitmap);
bitmap.fillRect(new Rectangle(0, 0, 50, 50), 0xff0000ff);

Thank you for reading.


Working with Bitmaps in Actionscript 3: Part 1
Working with Bitmaps in Actionscript 3: Part 3

No comments:

Post a Comment