Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Working with Images from the Library

Working with Images from the Library

You can create a bitmap object directly from an image in the Library that has a linkage identifier assigned using the BitmapData.loadBitmap() method. This method is a static method of the BitmapData class, which means it cannot be accessed from an instance of the class. Instead, you call it directly from the class itself and it returns a pre-populated instance that has the correct dimensions and contains all the pixel data.

To assign a linkage identifier to an image in the Library, use the following steps:

  1. Right-click the image in the Library and choose Linkage.
  2. Select the Export for ActionScript check box.
  3. Enter a unique name in the Identifier text box.
  4. Click Ok.

The following ActionScript creates a bitmap from an image in the Library that has been assigned a linkage identifier called logo.

import flash.display.BitmapData
logo = BitmapData.loadBitmap("logo")

The symbol is drawn as the bitmap object without any transformations applied. If you want to resize the symbol you loaded from the library it is best to attach the symbol inside a temporary movie clip using Movieclip.attachMovie() and then take a snapshot of the movie clip and its contents with a transformation matrix that scales down the bitmap:

import flash.geom.Matrix
import flash.display.BitmapData

//attach the movieclip from the
//library this.attachMovie("largeSymbol","temp_mc",
//this.getNextHighestDepth()

scale=50 //scale percentage
scale/=100

//create a transformation matrix that will scale the bitmap
scaleMatrix = new Matrix()
scaleMatrix.scale(scale,scale)

scaledBitmap = new BitmapData(
temp_mc._width*scale,temp_mc._height*scale,true,0x00FFFFFF)
///draw the bitmap and scale it using
//the specified transformation matrix
scaledBitmap.draw(temp_mc,scaleMatrix)

//remove the movie clip, we don't need it anymore
temp_mc.removeMovieClip()

If you want to resize a bitmap loaded from the library, then you cannot attach it to a movie clip using Movieclip.attachMovie(). You have to find another solution, which involves the use of two bitmap objects—one that will contain the unscaled bitmap and another that will contain the scaled bitmap:

import flash.geom.Matrix
import flash.display.BitmapData

//attach a bitmap from the library into a bitmap object
largeBitmap=BitmapData.load("largeBitmap")

scale=50 //scale percentage

//normzalize the scale
scale/=100

//create a new transformation matrix
scaleMatrix = new Matrix()
//scale the matrix
scaleMatrix.scale(scale,scale)
/*

If the attached bitmap is transparent,
fill the bitmap with transparent pixels
otherwise fill the bitmap with white pixels

*/

fillColor=(largeBitmap.transparent) ? 0x00FFFFFF : 0xFFFFFFFF

//
scaledBitmap = new BitmapData(
largeBitmap.width*scale,largeBitmap.height*scale,
largeBitmap.transparent,fillColor)
scaledBitmap.draw(largeBitmap,scaledMatrix)

//free the memory that the large bitmap
//is using as you don't need it anymore
largeBitmap.dispose()

Comments