Recipe 8.1. Creating a BitmapData Object


Problem

You want to create a new bitmap in your application.

Solution

Use the BitmapData class's constructor to create a new BitmapData object.

Discussion

The BitmapData class represents the pixels in a bitmap image, and contains many built-in methods for adding content to and manipulating that image. The first step is to create an instance of the class by calling its constructor, as follows:

var bitmap:BitmapData = new BitmapData(width, height,                                      transparent, fillColor);

This class is part of the flash.display package, so make sure you import flash.display.BitmapData at the top of the file. The width and height parameters specify what size bitmap you want to create. The next parameter is a Boolean value, which specifies whether the bitmap is created with an alpha channel (true) or not (false), and the fillColor determines the initial background color of the image.

Although width and height are mandatory, transparent and fillColor default to true and 0xFFFFFFFF, respectively if not explicitly passed to the constructor.

The fillColor accepts a 32-bit color value, which means that it supports an alpha channel. Of course, the alpha channel of the fill color is only relevant if you create the BitmapData specifying transparent as true. Otherwise, all colors are treated as 100 percent opaque.

The following example creates a BitmapData that is initially completely transparent, as the alpha channel of the fillColor is set to zero:

var bitmap:BitmapData = new BitmapData(100, 100,                                     true, 0x00FFFFFF);

After you create a new BitmapData, it exists only in memory. Although you can create content in the image and manipulate it, it will not be visible until you add it to the display list.

See Also

Recipe 8.2 for information on how to make a BitmapData visible.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net