The BitmapData Class


The BitmapData Class

The BitmapData class is designed to be able to load and manipulate bitmaps at runtime.

It is important that when using this class to import it first at the beginning of your script, like this:

 import flash.display.BitmapData; 

To instantiate a new instance of the BitmapData class, use this code as a template:

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

  • width The width of the bitmap in pixels.

  • height The height of the bitmap in pixels.

  • transparent A Boolean value representing transparency of the bitmap.

  • fillColor The hex color value representing the fill color of the bitmap.

Properties

height

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.height;

Description:

A numerical value representing the height of the bitmap in pixels.

Example:

This example will trace the height of a bitmap:

 import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30); trace(myBitmapData.height); //output: 30 

rectangle

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.rectangle;

Description:

A Rectangle object holding position and dimension data about a given bitmap. The top (y) and left (x) are zero.

Example:

This example will trace the information stored in the rectangle property:

 import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30); trace(myB.rectangle); //output: (x=0, y=0, w=20, h=30) 

transparent

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.transparent;

Description:

A Boolean value representing the state of transparency in a bitmap.

Example:

This example will trace transparency in a given bitmap:

 import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30,true); trace(myBitmapData.transparent); //output: true 

width

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.height;

Description:

A numerical value representing the width of the bitmap in pixels.

Example:

This example will trace the width of a bitmap:

 import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(20,30); trace(myBitmapData.width); //output: 20 

Methods

applyFilter

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.applyFilter(bitmap, rec, point, filter);

Parameters:

bitmap The source bitmap image having the filter applied to it. It can be either the current bitmap instance or a different bitmap.

rec A Rectangle object that will define the area to apply the filter to.

point A Point object that represents the top-left point of the applied area.

filter A Filter object representing the type of filter to be applied to the bitmap.

Returns: Number

If the filter has been applied successfully, the number 0 will be returned; otherwise a negative number will be returned.

Description:

This method will apply a defined filter to the designated bitmap.

Example:

This example will attach an image residing in the library and apply a Blur filter to it:

 import flash.display.BitmapData; import flash.geom.Point; import flash.filters.BlurFilter; var point:Point = new flash.geom.Point(0,0); var blur:BlurFilter = new flash.filters.BlurFilter(5, 5, 100); var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //apply the filter myBitmapData.applyFilter(myBitmapData, myBitmapData.rectangle, point, blur); //attach the image this.attachBitmap(myBitmapData, 1); 

clone

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.clone();

Returns: BitmapData

This method will return an exact duplicate of the BitmapData object it is being called on.

Description:

This method will create an exact copy of the bitmap image it is being called on.

Example:

This example will create a generic bitmap image, create a copy, and attach that copy:

 import flash.display.BitmapData; var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //create the copy var copyBitmap:BitmapData = myBitmapData.clone(); //attach the bitmap this.attachBitmap(copyBitmap,1); 

colorTransform

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.colorTransform(rec, transformObj);

Parameters:

rec A Rectangle object that will define the area to apply the color transform to.

transformObj A ColorTransform object used to adjust the color of the bitmap.

Description:

This method will apply a defined ColorTransform object to the designated bitmap.

Example:

This example will create a red rectangle that we will convert to a green rectangle with a ColorTransform object:

 import flash.display.BitmapData; import flash.geom.ColorTransform; //create a red rectangle var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //create the color transform var myTransform:ColorTransform = new ColorTransform(100,100,100,1,255,150,100,1); //apply the transform myBitmapData.colorTransform(myBitmapData.rectangle,myTransform); //attach the bitmap this.attachBitmap(myBitmapData,1); 

copyChannel

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.copyChannel(bitmap, rec, point, sourceChannel, destChannel);

Parameters:

bitmap The source BitmapData object where the channel will be copied from.

rec A Rectangle object that will define the area the new channel will be applied to.

point A Point object that represents the top left point of the applied area.

sourceChannel The source channel that is being copied from bitmap.

destChannel The destination channel where the sourceChannel will be copied to.

Description:

This method will copy a channel from one BitmapData instance to another. Here are the available channels:

  • 1 red

  • 2 green

  • 4 blue

  • 8 alpha

Example:

This example will create a red rectangle and then create a blue rectangle. After that, the blue channel will be copied from the second rectangle into the first, making it a purple rectangle. Then the rectangle will be displayed using the attachBitmap method:

 import flash.display.BitmapData; import flash.geom.Point; //create the point var point:Point = new Point(0,0); //create a red rectangle var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //create a second BitmapData object var myBitmapData2:BitmapData = new BitmapData(100,100,false,0x0000ff); //copy the blue channel over myBitmapData.copyChannel(myBitmapData2,myBitmapData.rectangle, point, 4, 4); //attach the bitmap this.attachBitmap(myBitmapData,1); 

copyPixels

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.copyPixels(bitmap, rec, point, aBitmap, aPoint, merge);

Parameters:

bitmap The source BitmapData object where the pixels will be copied from.

rec A Rectangle object that will define the area the new pixels will be applied to.

point A Point object that represents the top left point of the applied area.

aBitmap An optional second BitmapData object to use as the alpha source.

aPoint An optional Point object parameter representing the top left of the aBitmap parameter to use on the rec parameter.

merge An optional Boolean value that states to use the alpha channel (true) or not (false).

Description:

This method will copy over a selection of pixels from one BitmapData object to another:

Example:

This example will load two bitmaps that currently reside in the library (both 400x300 in size) and copy the one on the top half into the other to display:

 import flash.display.BitmapData; import flash.geom.*; //create the rectangle to only grab the top half var rec:Rectangle = new Rectangle(0,0,400,150); //top left var point:Point = new Point(0,0); //create the first bitmap object var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //create a second BitmapData object var myBitmapData2:BitmapData = BitmapData.loadBitmap("bigBen"); //copy the top half of the second rectangle over myBitmapData.copyPixels(myBitmapData2, rec, point); //attach the bitmap this.attachBitmap(myBitmapData,1); 

dispose

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.dispose();

Description:

This method will free up all memory in the instance of the BitmapData object.

Example:

This example will create a generic bitmap image, attach it to the root timeline so it is visible, and then set up an onMouseDown event so that when a user clicks the stage, the rectangle will be gone, and the memory will have been freed:

 import flash.display.BitmapData; //create the bitmap object var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //attach the bitmap this.attachBitmap(myBitmapData,1); //when the user clicks the mouse, clear the memory this.onMouseDown = function():Void{     myBitmapData.dispose(); } 

draw

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.draw(bitmap, matrix, transformObj, blendMode, clipRec, smooth);

Parameters:

bitmap The BitmapData object that will be drawn.

matrix A Matrix object used to control visual aspects of the bitmap (that is, rotation and scaling).

transformObj A ColorTransform object used to adjust the color of the bitmap.

blendMode A BlendMode object used to control the blend properties of the bitmap.

clipRec An optional Rectangle object parameter representing the clipping region.

smooth An optional Boolean value that states to smooth the bitmap if it scales (true) or not (false). The default is false.

Description:

Using vector rendering, this method will draw an image based on bitmap and other parameters you pass it.

Example:

This example will create a red square as the first bitmap. Then it will load in a bitmap from the library. The first bitmap will automatically display when tested, and when a user clicks the mouse, the second bitmap will be drawn in its place.

 import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = new BitmapData(100,100,false,0xff0000); //load the second bitmap from the Library var myBitmapData2:BitmapData = BitmapData.loadBitmap("bigBen"); //display the first bitmap this.attachBitmap(myBitmapData,1); //when a user clicks the stage, draw the second one this.onMouseDown = function(){     myBitmapData.draw(myBitmapData2); } 

fillRect

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.fillRect(rec, color);

Parameters:

rec The Rectangle object that defines the area of the bitmap to be filled.

color An ARGB value that represents the color the rectangle will be filled with.

Description:

This method will fill a defined Rectangle with the ARGB (0xAARRGGBB) color passed to it.

Example:

This example will create a generic bitmap image with no defined color. It will then fill the entire rectangle with a semitransparent red color. Then the bitmap will be attached to the main timeline:

 import flash.display.BitmapData; //create the bitmap object var myBitmapData:BitmapData = new BitmapData(100,100); //fill the rectangle myBitmapData.fillRect(myBitmapData.rectangle, 0x88ff0000); //attach the bitmap this.attachBitmap(myBitmapData,1); 

floodFill

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.floodFill(x, y, color);

Parameters:

x The horizontal coordinate of the bitmap.

y The vertical coordinate of the bitmap.

color An ARGB (0xAARRGGBB) value that represents the color the rectangle will be filled with.

Description:

This method will carry out a flood-fill operation beginning at the designated coordinates.

Example:

This example will first import the necessary class file. Then we will create an initial color to work with later in the example. After that, we create a new bitmap, set its color to a light gray, and then attach it to the root timeline. Finally, we create an event that is triggered each time the user clicks the stage and that will incrementally increase the startColor variable; then, using the floodFill method, it changes the color of the rectangle to gradually appear more red:

 import flash.display.BitmapData; //create the initial color var startColor:Number = 0x00110000; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0x00bbbbbb); //attach the bitmap this.attachBitmap(myBitmapData, 1); //incrament the color and fill the rectangle this.onMouseDown = function():Void{         startColor += 0x00100000;         myBitmapData.floodFill(0, 0, startColor); } 

generateFilterRect

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.generateFilterRect(rec, filter);

Parameters:

rec The Rectangle object used to define the designated area to have the filter applied to it.

filter The Filter object that will be applied.

Returns: Rectangle

The Rectangle object returned will represent the necessary dimensions and placement to fully display the bitmap when the given filter is applied.

Description:

This method will generate the Rectangle necessary to accommodate the bitmap if it is applied to the given filter.

Example:

This example will generate the necessary Rectangle to use if a BlurFilter is applied to a 400x300 image:

 import flash.display.BitmapData; import flash.filters.BlurFilter; //create the filter var blur:BlurFilter = new flash.filters.BlurFilter(5, 5, 100); //load the bitmap var myBitmapData:BitmapData =  BitmapData.loadBitmap("bigBen");//an image in the library //generate the rectangle trace(myBitmapData.generateFilterRect(myBitmapData.rectangle, blur)); //output: (x=-18, y=-18, w=436, h=336) 

getColorBoundsRect

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.getColorBoundsRect(maskColor, color, findColor, filter);

Parameters:

maskColor An ARGB hexadecimal color.

color An ARGB hexadecimal color.

findColor A Boolean value that if TRue, returns the bounding Rectangle where color exists; if false, will return the bounding Rectangle where color does not exist.

Returns: Rectangle

The Rectangle object returned will represent the minimum size rectangle to encompass all pixels of the bitmap that contain the given color.

Description:

This method will search the bitmap image for the given hexadecimal color and return a bounding rectangle capable of encompassing all pixels with that color.

Example:

This example will generate the necessary Rectangle to encompass a color at a given pixel (10,10):

 import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData =  BitmapData.loadBitmap("bigBen");//an image in the library //generate the rectangle trace(myBitmapData.getColorBoundsRect (0xffffffff, myBitmapData.getPixel32(10,10), true)); //output: (x=8, y=10, w=32, h=34) 

getPixel

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.getPixel(x, y);

Returns: Number

The RGB value of a given pixel.

Parameters:

x The horizontal coordinate of the bitmap.

y The vertical coordinate of the bitmap.

Description:

This method will return the RGB value of a pixel at coordinate (x,y).

Example:

This example will load a bitmap from the library, then return the color of the pixel at coordinate (100,100). After that, we will use the fillRect method to completely color the bitmap with the selected color. And finally, the block of color will be displayed.

 import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //grab the color of the pixel at (100,100) var tempColor:Number = myBitmapData.getPixel(100,100); //fill the rectangle with that color myBitmapData.fillRect(myBitmapData.rectangle, tempColor); //display the color block this.attachBitmap(myBitmapData, 1); 

getPixel32

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.getPixel32(x, y);

Returns: Number

The ARGB value of a given pixel.

Parameters:

x The horizontal coordinate of the bitmap.

y The vertical coordinate of the bitmap.

Description:

This method will return the ARGB value of a pixel at coordinate (x,y).

Example:

This example will load a bitmap from the library, then return the color of the pixel at coordinate (100,100). After that, we use the fillRect method to completely color the bitmap with the selected color. And finally, the block of color will be displayed.

 import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library //grab the color of the pixel at (100,100) var tempColor:Number = myBitmapData.getPixel32(100,100); //fill the rectangle with that color myBitmapData.fillRect(myBitmapData.rectangle, tempColor); //display the color block this.attachBitmap(myBitmapData, 1); 

hitTest

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.hitTest(point, alpha, obj, bitmapPoint, alphaThreshhold);

Parameters:

point A Point object representing a point in the bitmap.

alpha The maximum alpha that is still considered opaque.

obj A Rectangle, Point, or BitmapData object.

bitmapPoint An optional parameter representing the Point in the second bitmap image, if BitmapData is the obj data type.

alphaThreshold The maximum alpha that is still considered opaque in the second bitmap image, if BitmapData is the obj data type.

Returns: Boolean true if the hit is within designated area, otherwise false.

Description:

This method will return a Boolean value answering whether a hit has occurred in a designated area of a bitmap.

Example:

This example will first get the necessary classes we need. Then a bitmap will be created with a red square being drawn. Next, the bitmap will be attached to the root timeline. And finally, we use the onMouseMove event to monitor whether the user moves the mouse over the rectangle:

 import flash.display.BitmapData; import flash.geom.Point; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0x00FF0000); //attach the bitmap this.attachBitmap(myBitmapData, 1); //monitor the mouse position this.onMouseMove = function():Void{    if(myBitmapData.hitTest(new Point(), 255, new Point(_xmouse, _ymouse))) {       trace("x - " + _xmouse + " y - " + _ymouse);    } } 

loadBitmap

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.loadBitmap(id);

Parameters:

id The linkage identifier of a bitmap in the library.

Returns: BitmapData

The BitmapData information for the loading bitmap.

Description:

This method will load a bitmap from the library into a BitmapData object at runtime.

Example:

This example will load a bitmap from the library and display it in the root timeline.

 import flash.display.BitmapData; //load the bitmap var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen");//an image in the library this.attachBitmap(myBitmapData, 1); 

merge

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.merge(bitmap, rec, point, red, green, blue, alpha);

Parameters:

bitmap The bitmap image that will have the merge applied to it. It does not have to be the BitmapData object calling the method.

rec The Rectangle that defines the area where the merge will take effect.

point The Point object representing the top left of the current BitmapData object that corresponds to the top left of rec.

red The number to multiply the red channel by.

green The number to multiply the green channel by.

blue The number to multiply the blue channel by.

alpha The number to multiply the alpha channel by.

Description:

This method will join two BitmapData objects and combine their channels based on the amount of multiplication given.

Example:

This example will create a red and a blue bitmap, then combine them and increase the blue by 255. Then the bitmap will be displayed as purple:

 import flash.display.BitmapData; import flash.geom.Point; //create the point var point:Point = new Point(0,0); //create both bitmaps var myBitmapData:BitmapData = new BitmapData(100,100,false, 0xff0000); var myBitmapData2:BitmapData = new BitmapData(100,100,false, 0x0000ff); //merge the 2 and increase the blue myBitmapData.merge(myBitmapData2, myBitmapData.rectangle, point, 0, 0, 255, 0); //display the bitmap this.attachBitmap(myBitmapData, 1); 

noise

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.noise(seed, low, high, channels, grayScale);

Parameters:

seed A random number to create a random feeling noise.

low The minimum value per channel between 0255. Default value is 0.

high The maximum value per channel between 0255. Default value is 255.

channels This represents which channels to use; 1, 2, 4, or 8. You can also combine them using the logical OR operator (|) like this (1|4|8). The default value is (1|2|4).

grayScale A Boolean value determining whether to use a grayscale image. The default value is false.

Description:

This method will fill a bitmap with pixels representing random noise (static).

Example:

This example will create a small rectangle of constantly changing static:

 import flash.display.BitmapData; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100,100); //constantly update the noise this.onEnterFrame = function(){     myBitmapData.noise(Math.random()*100, 1, 255, 1, true); } //display the static this.attachBitmap(myBitmapData,1); 

paletteMap

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.paletteMap(bitmap, rec, point, redArray, greenArray, blueArray, alphaArray);

Parameters:

bitmap The bitmap image that will have the paletteMap applied to it. It does not have to be the BitmapData object calling the method.

rec The Rectangle that defines the area where the paletteMap will take affect.

point The Point object representing the top left of the current BitmapData object that corresponds to the top left of rec.

redArray The array of values to use with the red channel.

greenArray The array of values to use with the green channel.

blueArray The array of values to use with the blue channel.

alphaArray The array of values to use with the alpha channel.

Description:

This method will re-map each of the four channels in a given bitmap using four arrays each containing 256 values.

Example:

This example will first import the necessary class files. Then we will create a variable to hold an initial height that we will use later. Next, we create the bitmap as a 100x100 red square. Then the bitmap is attached to the root timeline. Finally, we use the onMouseDown event to create blue, horizontal stripes down the red square:

[View full width]

import flash.display.BitmapData; import flash.geom.Rectangle; import flash.geom.Point; //create the start height variable var startHeight = 2; //create the bitmap var myBitmapData:BitmapData = new BitmapData(100, 100, false, 0x00FF0000); //attach the bitmap this.attachBitmap(myBitmapData, 1); //make some stripes this.onMouseDown = function():Void{ var red_array:Array = new Array(); red_array[255] = 0x000000FF; var blue_array:Array = new Array(); blue_array[255] = 0x00FF0000; myBitmapData.paletteMap(myBitmapData, new Rectangle(0, 0, 100, startHeight), new Point (), red_array, undefined, blue_array); startHeight+=2; }

perlinNoise

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.perlinNoise(baseX, baseY, octaves, seed, stitch, fractal, channels, grayscale, offSet);

Parameters:

baseX The horizontal frequency.

baseY The vertical frequency.

octaves The total number of individual noise functions. The higher this number, the more detailed and the more processor-intensive the bitmap will become.

seed A random number used to create a realistic noise effect.

stitch A Boolean value that if true will attempt to smooth the transition edges in the bitmap image.

fractal A Boolean value that if TRue will generate fractal noise, but if false will create turbulence.

channels This represents which channels to use; 1, 2, 4, or 8. You can also combine them using the logical OR operator (|) like this (1|4|8). The default value is (1|2|4).

grayScale A Boolean value determining whether to use a grayscale image. The default value is false.

offSet The array of values that correspond to x, y coordinates that can smoothly scroll the layers.

Description:

This method will create a more advanced form of noise than the noise method does. Images made with perlinNoise are often used for things such as fire or water.

Example:

This example will create a small lightning effect in a BitmapData object:

 import flash.display.BitmapData; //create the bitmap var myBitmapData:BitmapData = new BitmapData(200, 200); //make lightning this.onEnterFrame = function(){     myBitmapData.perlinNoise(50, 50, 5, Math.random()*100,  false, false, 8, false); } //display the lightning this.attachBitmap(myBitmapData,1); 

pixelDissolve

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.pixelDissolve(bitmap, rec, point, seed, numPixels, fillColor);

Returns: Number

This number represents the next random seed that should be used.

Parameters:

bitmap The bitmap image that will have the pixelDissolve applied to it. It does not have to be the BitmapData object calling the method.

rec The Rectangle that defines the area where the pixelDissolve will take effect.

point The Point object representing the top left of the current BitmapData object that corresponds to the top left of rec.

seed A random number used to begin the dissolve. The default value is 0.

numPixels The number of pixels to dissolve. The default value is 1/30 of the overall area.

fillColor An ARGB color used to fill pixels.

Description:

This method will render a pixel dissolve effect on the given bitmap using a second bitmap.

Example:

This example will load two bitmaps from the library, display the first, then continually dissolve in the second one.

[View full width]

import flash.display.BitmapData; import flash.geom.Point; //create the point var point:Point = new Point(0,0); //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //load the second bitmap from the Library var myBitmapData2:BitmapData = BitmapData.loadBitmap("bigBen"); //display the first bitmap this.attachBitmap(myBitmapData,1); //randome seed var nextNum:Number = 0; //run this continually this.onEnterFrame = function():Void{ nextNum = myBitmapData.pixelDissolve(myBitmapData2, myBitmapData.rectangle, point ,nextNum,500,0x000000); }

scroll

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.scroll(x, y);

Parameters:

x The horizontal amount to scroll of the bitmap.

y The vertical amount to scroll of the bitmap.

Description:

This method will scroll an image by the pixel amount given.

Example:

This example will load a bitmap from the library, display that bitmap, and finally scroll it by (1,1) continually creating a color stream effect.

 import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //display the first bitmap this.attachBitmap(myBitmapData,1); //continually scroll the image this.onEnterFrame = function():Void{     myBitmapData.scroll(1,1); } 

setPixel

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.setPixel(x, y, color);

Parameters:

x The horizontal position of the pixel to color.

y The vertical position of the pixel to color.

color The RGB value to set the given pixel to.

Description:

This method will set the RGB value of a given pixel.

Example:

This example will load a bitmap from the library, display that bitmap, and create an event that allows the user to color every pixel the mouse touches to a random color.

 import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); //display the first bitmap this.attachBitmap(myBitmapData,1); //continually scroll the image this.onMouseMove = function():Void{     myBitmapData.setPixel(_xmouse, _ymouse, Math.random()*0x1000000); } 

setPixel32

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.setPixel32(x, y, color);

Parameters:

x The horizontal position of the pixel to color.

y The vertical position of the pixel to color.

color The ARGB (0xAARRGGBB) value to set the given pixel to.

Description:

This method will set the ARGB value of a given pixel.

Example:

This example will load a bitmap from the library and make a copy in a second BitmapData object. Then two variables will be created to hold the height and width of the bitmap. After that, the bitmap will be displayed. Then two more variables will be created to help walk through the image. And finally, an onEnterFrame event will be used to completely flip the bitmap both vertically and horizontally, pixel by pixel.

 import flash.display.BitmapData; //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bridge"); var myBitmapData2:BitmapData = myBitmapData.clone() var w:Number = myBitmapData.width; var h:Number = myBitmapData.height; //display the first bitmap this.attachBitmap(myBitmapData,1); var column:Number = 0; var row:Number = 0; //continually scroll the image this.onEnterFrame = function():Void{     if(column>w){         column=0;         row++;     }     myBitmapData.setPixel32(column, row, myBitmapData2.getPixel32(w-column,h-row));     column++; } 

threshold

Availability: FP:8, AS:1.0

Generic Template: myBitmapData.threshold (bitmap, rec, point, operation, threshold, color, mask, copy);

Returns: Number

This number is the number of pixels that were changed.

Parameters:

bitmap The bitmap image that will have the threshold applied to it. It does not have to be the BitmapData object calling the method.

rec The Rectangle that defines the area where the threshold will take effect.

point The Point object representing the top left of the current BitmapData object that corresponds to the top left of rec.

operation A comparison operator used to compare different colors.

threshold The color value that each pixel will be tested against to see if it meets or goes beyond the threshold.

color An ARGB color used to fill pixels where the threshold test succeeds. The default is 0x00000000.

mask An ARGB color used to isolate color components. The default value is 0xffffffff.

copy If this value is true, pixels will be copied from the source bitmap to the destination bitmap when the threshold is not met. If false, no copies are made. The default value is false.

Description:

This method will test different pixel values in a given bitmap and decide whether to swap them out with a given ARGB color.

Example:

This example loads a bitmap image from the library and then displays it. After that, an event will be set up so that when a user selects a pixel by clicking it, every pixel in the bitmap will be compared to that pixel and colored blue if the threshold test succeeds.

[View full width]

import flash.display.BitmapData; import flash.geom.Point; var point:Point = new Point(0,0); //create the first BitmapData as a red square var myBitmapData:BitmapData = BitmapData.loadBitmap("bigBen"); //display the first bitmap this.attachBitmap(myBitmapData, 1); //select a pixel this.onMouseDown = function(){ myBitmapData.threshold(myBitmapData, myBitmapData.rectangle, point, "<=", myBitmapData .getPixel32(_xmouse,_ymouse), 0x00397dce, 0xffffffff, false); }




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

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