BitmapData.loadBitmap Gone in Actionscript 3

BitmapData.loadBitmap Gone in Actionscript 3When playing with the BitmapData API in Flash 8, it was often necessary to work with an image embedded in the library. To achieve this you could assign a 'Linkage Identifier' to the image in the library and then use BitmapData.loadBitmap to retrieve a BitmapData instance pre-populated with the specified image from the library:

import flash.display.BitmapData;
myBmp = BitmapData.loadBitmap('theLinkageId');

Naturally things have changed somewhat in Actionscript 3. There are no longer Linkage Identifiers and there is no longer attachMovie. Everything is created using the new operator.

So, how do you go about embedding an image inside a Flash Movie in Actionscript 3 you might ask? and then how do you get a BitmapData object for that embedded Image? Well im going to show you because these questions seem to be coming up alot of late.

First things first, to embed external assets such as sound, other flash movies or images inside of an Actionscript 3 or Flex Projects you use the
Embed metadata like so:

[Embed(source='myPhoto.png')]

You then need to assign this embedded asset a unique class name by declaring a variable underneath the metadata:

package
{

import flash.display.Sprite;

class EmbeddedImageExample extends Sprite
{
[Embed(source='myPhoto.png')]
public var MyPhoto:Class;

function EmbeddedImageExample()
{
}
}
}

When you compile your Project, the MXML Compiler actually generates an Actionscript class which contains both the name of the class that the image is embedded inside and the class name that you defined in your variable. Therefore in the above example, the MXML Compiler would generate an actionscript class called EmbeddedImageExample_MyPhoto.

The generated class for an embedded image will always extend the mx.core.SkinBitmap class (Currently not documented), which is basically a flash.display.Bitmap that also implements the IFlexDisplayObject interface.

Back to our example, the variable MyPhoto will be populated with a reference to the automatically generated class, in our case a class called EmbeddedImageExample_MyPhoto, hence why we declare the variable MyPhoto to be of type Class. We can then create an instance of this automatically generated class using the new operator as follows:

package
{
import flash.display.Bitmap;
import flash.display.Sprite;

class EmbeddedImageExample extends Sprite
{

[Embed(source='myPhoto.png')]
public var MyPhoto:Class;

function EmbeddedImageExample()
{
var pic:Bitmap = new MyPhoto();
}
}
}

In the above code, we now have a variable called pic that contains a Bitmap instance. A Bitmap is a subclass of DisplayObject thus it can be added directly to the display list:

package
{
import flash.display.Bitmap;
import flash.display.Sprite;

class EmbeddedImageExample extends Sprite
{

[Embed(source='myPhoto.png')]
public var MyPhoto:Class;

function EmbeddedImageExample()
{
var pic:Bitmap = new MyPhoto();
this.addChild(pic);
}
}
}

The Bitmap class points to a particular BitmapData object, which is what is actually shown, you can have multiple Bitmap instances pointing to the same BitmapData object. BitmapData objects use alot of memory, if you want to show the same BitmapData object in two different places you can create two seperate Bitmap instances that point to the one BitmapData object. This saves memory because you dont need two copies of the same BitmapData object.

To access the BitmapData object that is referenced by the Bitmap instance we have created, do the following:

package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;

class EmbeddedImageExample extends Sprite
{

[Embed(source='myPhoto.png')]
public var MyPhoto:Class;

function EmbeddedImageExample()
{
var pic:Bitmap = new MyPhoto();
var bmp:BitmapData = pic.bitmapData;
}
}
}

And there you have it.


Close    To Top
  • Prev Article-Flash:
  • Next Article-Flash:
  • Now: Tutorial for Web and Software Design > Flash > Basic > Flash Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction