Turn your BitmapData into a string and back! Great for storing images in XML, Databases, or transmitting them over a network. This post will cover Bitmap Encoding, PNG Encoding and JPG Encoding along with the pros and cons for each.

Bitmap Encoding – (link)

// Encode
var encoded:String = BitmapEncoder.encodeBase64(bitmap.bitmapData);
	
// Decode
var bd:BitmapData = BitmapEncoder.decodeBase64(encoded);

Advantages:
Keep transparency
Fastest Method
Does not require a Loader

Disadvantages:
Largest Filesize
Will only work with images up to a certain size

PNG Encoding – (link)

// Encode  	
var ba:ByteArray = PNGEncoder.encode(bitmap.bitmapData);
var encoded:String = Base64.encodeByteArray(ba);

// Decode  	
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
var loader:Loader = new Loader();
loader.loadBytes(decoded);

Advantages:
Keep transparency
Smaller filesize than Bitmap Encoding

Disadvantages:
Slower than Bitmap Encoding
Larger than JPG Encoding
Requires Loader

JPG Encoding – (link)

// Encode
var jpgEncoder:JPGEncoder = new JPGEncoder();
var ba:ByteArray = jpgEncoder.encode(bitmap.bitmapData);
var encoded:String = Base64.encodeByteArray(ba);

// Decode
var decoded:ByteArray = Base64.decodeToByteArray(encoded);
var loader:Loader = new Loader();
loader.loadBytes(decoded);

Advantages:
Smallest Filesize
Variable Quality

Disadvantages:
Slower than Bitmap Encoding
No Transparency
Requires Loader

5 thoughts on “Base64 Encoding BitmapData

  1. For the bitmap converted into a String, what about using the core zip library and re-encode it in base 64?
    It might be slower but the size should be smaller.
    Something to consider?

Comments are closed.