The following example shows how you can dynamically load an image at runtime using the ActionScript 3.0 Loader class and then copy the image’s pixels into four separate Bitmap object instances on the display list by using the Loader instance’s content property and bitmapData properties.

Full code after the jump.


// ActionScript 3.0
const IMAGE_URL:String = "http://www.helpexamples.com/flash/images/logo.png";
 
var ldr:Loader = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest(IMAGE_URL));
 
var bitmap1:Bitmap;
var bitmap2:Bitmap;
var bitmap3:Bitmap;
var bitmap4:Bitmap;
 
function ldr_complete(evt:Event):void {
    var bmp:Bitmap = ldr.content as Bitmap;
 
    bitmap1 = new Bitmap(bmp.bitmapData);
    bitmap1.x = 100;
    bitmap1.y = 100;
    bitmap1.rotation = 0;
    addChild(bitmap1);
 
    bitmap2 = new Bitmap(bmp.bitmapData);
    bitmap2.x = 200;
    bitmap2.y = 100;
    bitmap2.rotation = 90;
    addChild(bitmap2);
 
    bitmap3 = new Bitmap(bmp.bitmapData);
    bitmap3.x = 300;
    bitmap3.y = 100;
    bitmap3.rotation = 180;
    addChild(bitmap3);
 
    bitmap4 = new Bitmap(bmp.bitmapData);
    bitmap4.x = 400;
    bitmap4.y = 100;
    bitmap4.rotation = 270;
    addChild(bitmap4);
}
 

4 Responses to Copying a dynamically loaded image’s pixels to a new Bitmap instance

  1. Wasabilover says:

    Thanks. This helped me alot.

  2. Sami says:

    Thanks for the snippet.

  3. Marcus says:

    Hi,

    you don’t happen to know, how to convert a base64 encoded image into bitmapData, so I can load it into a drawing area using the beginBitmapFill() method?

    I’m stuck on this topic and need some new input.

    Thanks in advance.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Spam Protection by WP-SpamFree