Collage FX Studio
PhotoMagic Studio
Photo Card Workshop
Magic Animator
Previous | This is last articleNext

ActionScript 3 class to convert BitmapData object

I think this is a useful class for web application. just a simple example, you can encode a BitmapData object into Base64 string  by the encodeBase64 method and save it into database,  when you need to use the BitmapData, use the decodeBase64 method to decode Base64 string into BitmapData object.

package com.foxarc.images {  
    import flash.display.BitmapData;  
    import flash.geom.Rectangle;  
    import flash.utils.ByteArray;          
    import com.foxarc.util.Base64;  
    public class BitmapEncoder {  
        public static function encodeByteArray(data:BitmapData):ByteArray{  
            if(data == null){  
                throw new Error("data parameter can not be empty!");  
            var bytes:ByteArray = data.getPixels(data.rect);  
            return bytes;  
        public static function encodeBase64(data:BitmapData):String{  
            return Base64.encodeByteArray(encodeByteArray(data));  
        public static function decodeByteArray(bytes:ByteArray):BitmapData{  
            if(bytes == null){  
                throw new Error("bytes parameter can not be empty!");  
            if(bytes.length <  6){  
                throw new Error("bytes parameter is a invalid value");  
            bytes.position = bytes.length - 1;  
            var transparent:Boolean = bytes.readBoolean();  
            bytes.position = bytes.length - 3;  
            var height:int = bytes.readShort();  
            bytes.position = bytes.length - 5;  
            var width:int = bytes.readShort();  
            bytes.position = 0;  
            var datas:ByteArray = new ByteArray();            
            bytes.readBytes(datas,0,bytes.length - 5);  
            var bmp:BitmapData = new BitmapData(width,height,transparent,0);  
            bmp.setPixels(new Rectangle(0,0,width,height),datas);  
            return bmp;  
        public static function decodeBase64(data:String):BitmapData{              
            return decodeByteArray(Base64.decodeToByteArray(data));  
        public function BitmapEncoder() {  
            throw new Error("BitmapEncoder  is a static class!");  

Comment: 0 | Read times: -
Announce commentary
Your name
Validation code Code