O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loading an External Bitmap Image

In the previous section, we learned how to create a brand new bitmap. Now let's try loading an existing bitmap image from disk. The types of bitmap images that can be loaded and displayed are: JPEG, GIF, and PNG.

Tip

Externally loaded JPEG images can be in progressive or nonprogressive format. Animated GIF images do not animate; only their first frame is displayed.

External bitmaps can be loaded in two ways: at runtime, using the Loader class, or at compile time, using the [Embed] metadata tag. For reference, Example 26-2 and Example 26-3 present sample code showing both techniques; for much deeper coverage, see Chapter 28.

Example 26-2 shows how to load a bitmap named photo.jpg at runtime. The code assumes that both the bitmap file and the .swf file loading the bitmap file are in the same directory.

Example 26-2. Loading a bitmap at runtime

package { import flash.display.*; import flash.events.*; import flash.net.*; // A simple example showing how to load an image public class BitmapLoader extends Sprite { private var loader:Loader; // The bitmap loader public function BitmapLoader() { // Create the loader loader = new Loader(); // Register to be notified when the bitmap has been loaded // and initialized loader.contentLoaderInfo.addEventListener(Event.INIT, initListener); // Load the bitmap loader.load(new URLRequest("photo.jpg")); } // Triggered when the bitmap has been loaded and initialized private function initListener (e:Event):void { // Add the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required