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)
[AS]
// Encode
var encoded:String = BitmapEncoder.encodeBase64(bitmap.bitmapData);
// Decode
var bd:BitmapData = BitmapEncoder.decodeBase64(encoded);
[/AS]
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)
[AS]
// 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);
[/AS]
Advantages:
Keep transparency
Smaller filesize than Bitmap Encoding
Disadvantages:
Slower than Bitmap Encoding
Larger than JPG Encoding
Requires Loader
JPG Encoding – (link)
[AS]
// 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);
[/AS]
Advantages:
Smallest Filesize
Variable Quality
Disadvantages:
Slower than Bitmap Encoding
No Transparency
Requires Loader
Tags: Base64, BitmapData







Baz
December 22nd, 2009
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?
uberVU - social comments
December 28th, 2009
Social comments and analytics for this post…
This post was mentioned on Twitter by howdous: Base64 Encoding BitmapData: Turn your BitmapData into a string and back! Great for storing images in XML, Databases… http://bit.ly/61yEpC…
Chris Black
December 31st, 2009
The string is the result of Base64 encoding the ByteArray. I’m not sure what the effect would be of Base64 encoding a Base64 encoded string
Arin Aivazian
August 22nd, 2011
Is there any as2 equivalent?
CJ DEEZ
October 5th, 2011
Thanks gonna try this now.