Actionscript, Old Articles - 2 years, 6 months ago - 3 Comments

BitmapData.loadBitmap gone in AS3

Tags: ,

When 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:

Actionscript:
  1. import flash.display.BitmapData;
  2. 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:

Actionscript:
  1. package
  2. {
  3.  
  4. import flash.display.Sprite;
  5.  
  6. class EmbeddedImageExample extends Sprite
  7. {
  8. [Embed(source='myPhoto.png')]
  9. public var MyPhoto:Class;
  10.  
  11. function EmbeddedImageExample()
  12. {
  13.  
  14. }
  15.  
  16. }
  17.  
  18. }

When you compile your Project, the MXML Compiler actually generates an Actionscript class (See Flex 2 Compilation - Hidden Goodies) 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:

Actionscript:
  1. package
  2. {
  3.  
  4. import flash.display.Sprite;
  5. import flash.display.Bitmap;
  6.  
  7. class EmbeddedImageExample extends Sprite
  8. {
  9. [Embed(source='myPhoto.png')]
  10. public var MyPhoto:Class;
  11.  
  12. function EmbeddedImageExample()
  13. {
  14. var pic:Bitmap = new MyPhoto();
  15. }
  16.  
  17. }
  18.  
  19. }

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:

Actionscript:
  1. package
  2. {
  3.  
  4. import flash.display.Sprite;
  5. import flash.display.Bitmap;
  6.  
  7. class EmbeddedImageExample extends Sprite
  8. {
  9. [Embed(source='myPhoto.png')]
  10. public var MyPhoto:Class;
  11.  
  12. function EmbeddedImageExample()
  13. {
  14. var pic:Bitmap = new MyPhoto();
  15. this.addChild(pic)
  16. }
  17.  
  18. }
  19.  
  20. }

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:

Actionscript:
  1. package
  2. {
  3.  
  4. import flash.display.Sprite;
  5. import flash.display.BitmapData
  6. import flash.display.Bitmap
  7.  
  8. class EmbeddedImageExample extends Sprite
  9. {
  10. [Embed(source='myPhoto.png')]
  11. public var MyPhoto:Class;
  12.  
  13. function EmbeddedImageExample()
  14. {
  15. var pic:Bitmap = new MyPhoto();
  16. var bmp:BitmapData=pic.bitmapData
  17. }
  18.  
  19. }
  20.  
  21. }

And there you have it.



3 Comments

You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

mike
Nov 8, 2007 3:09

Thanks for that update.

It would be nice to see more information on AS3 and bitmapData. I have a hard time putting my head around it, so to speak. And there just isn’t much of a primer out there :(

thanks.

Tek
Dec 10, 2007 14:16

Good reminder … bookmarked, thanks. ;)

izabela
Dec 20, 2007 22:46

this may shed more light on the BitmapData in AS 3.0:
http://www.adobe.com/devnet/flash/articles/image_api_02.html

cheers

Note: This is an old article!

Please be aware that the content of this article may no longer be accurate and the links contained within could be broken.

Update Notifications

You can add our RSS feed to your favorite feed reader or recieve an email when a new article is posted by entering your email address below.

    Shu Player allows you to distribute your AIR Applications to users that dont have the AIR Runtime by converting them to standalone applications. Shu Player also adds more commands to do things like open external applications etc...


    If you hate the Adobe Updater like most of us, then just get rid of it.


    Want to quickly find out what version of the Flash Player a client has installed? Send them to www.playerversion.com


    Yet more pv3d goodness. Im loving this funky 3D Flash Lab, the work of Mathieu Badimon. [Update: Apparently this uses Five3D not PaperVision]


    There is an old interview with me on The FWA, its fairly long at 8 pages.


    If you do alot of travelling then it makes sense to get yourself a Priority Pass, which will get you access to over 500 airport lounges worldwide, regardless of whether your flying enconomy, business or first.


    Freelance Switch is a freelancer community with forums, jobs, daily tips and a complete 212 page e-book titled "How to be a rockstar freelancer" for sale.


    Lots of new media jobs at Creative Pool.


    BBC IPlayer is awesome, but its ashame that BBC News is still using Windows Media Player and Real video. Real player is officially badware.


More In Actionscript


More In Adobe Flash


More In Freelancing