Discover Master of Alchemy, our first iPad/iPhone and iPod touch game!

Follow Me


Forum's topics

Latest Files


Top Rated


Photo Gallery

Export JPEG with Flash/PHP

1. Description

With the introduction of flash 8 "flash.display.BitmapData" class thousands of new application can now be made using Flash..
In this tutorial we will focus our attention to the BitmapData.gePixel() method in order to transform a portion of flash movie into a JPEG (created with PHP/GD)

BitmapData has different ways to get pixel color informations:

  • getPixel(x:Number, y:Number) : Number

    Returns an integer representing an RGB pixel value from a BitmapData object at a specific point (x, y).

  • getPixel32(x:Number, y:Number) : Number

    Returns an ARGB color value that contains alpha channel data as well as RGB data.

  • getColorBoundsRect(mask:Number, color:Number, [findColor:Boolean]) : Rectangle

    Determines a rectangular region that fully encloses all pixels of a given color within the bitmap image.

What we will use in this tutorial is getPixel().

Here a basic example on how it can work:

import flash.display.*

var bmp:BitmapData = new BitmapData(this._width, this._height, false)

this.onMouseMove = function(){
    var pColor:Number = bmp.getPixel(_xmouse, _ymouse)
    var hexColor:String = pColor.toString(16).toUpperCase()
    while(hexColor.length < 6){
        hexColor = "0" + hexColor
    var r = Number("0x" + hexColor.substr(0,2))
    var g = Number("0x" + hexColor.substr(2,2))
    var b = Number("0x" + hexColor.substr(4,2))
    testo.text = "0x" + hexColor + ", {r:" + r + ", g:" + g + ", b:" + b + "}"


Remember to import the flash.display.BitmapData class.
Create a new bitmapdata instance and assign to it the same dimensions as the current Stage.
Then using draw() we're making an exact copy of the _root movieclip into the bitmapdata object.
bmp.getPixel( _xmouse, _ymouse).toString(16).toUpperCase()
we will have the hexadecimal color value of the pixel at those coordinates.


2. Advanced Example

Ok, we can make a copy of everything in a flash movie using this method.. and so we can also send all the pixel color values to an external application in order to recreate a JPEG of the copied movieclip.
The bigger problem is that for a movie of 550x400 whe should collect 220000 colors values, and converting the value into an hexadecimal string this means that the final string to send will be 1320000 chars long. This means a lot of data!
Another problem is that we can't collect all the pixel color values in a single for loop, otherwise the flash player will dead suddenly!

I made this example ( 500x210 ), with a .flv video included.
You can also draw some lines above the .swf using mouse