blog
Bitmap, BitmapData, ByteArray… at Jozef Chúťka's blog
October 15, 2009
Bitmap BitmapData ByteArray converting DisplayObject encoding image Loader LoaderInfo url URLRequest
Bitmap, BitmapData, ByteArray…
Lets see how to convert between Bitmap, BitmapData and ByteArray in few examples:
DisplayObject to BitmapData
BitmapData to Bitmap
BitmapData to ByteArray
URL (image) to ByteArray (asynchronous)
URL (image) to BitmapData (asynchronous)
ByteArray to BitmapData (asynchronous)
DisplayObject to BitmapData. FYI DisplayObject is any visible object in flash (Sprite, MovieClip, Bitmap, UIComponent, Video etc…):
import flash.display.BitmapData;
var bitmapData:BitmapData = new BitmapData(button.width, button.height, false, 0xFFFFFF);
bitmapData.draw(button);
// result: bitmapDataBitmapData to Bitmap:
import flash.display.Bitmap;
var bitmap:Bitmap = new Bitmap(bitmapData);
// result: bitmapBitmapData to ByteArray. Requires jpeg / png or any other encoder. Read more about JPEG encoding here:
import mx.graphics.codec.JPEGEncoder;
import flash.utils.ByteArray;
var encoder:JPEGEncoder = new JPEGEncoder(90);
var byteArray:ByteArray = encoder.encode(bitmapData));
// result: byteArrayURL (image) to ByteArray. Asynchronous operation:
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.ByteArray;
var loader:Loader = new Loader();
loader.load(new URLRequest("banner1.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
private function loaderComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
var byteArray:ByteArray = loaderInfo.bytes;
// result: byteArray
}URL (image) to BitmapData. Asynchronous operation:
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.BitmapData;
var loader:Loader = new Loader();
loader.load(new URLRequest("banner1.jpg"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
private function loaderComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
var bitmapData:BitmapData = new BitmapData(loaderInfo.width, loaderInfo.height, false, 0xFFFFFF);
bitmapData.draw(loaderInfo.loader);
// result: bitmapData
}ByteArray to BitmapData. Asynchronous operation:
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.display.BitmapData;
var loader:Loader = new Loader();
loader.loadBytes(byteArray);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
private function loaderComplete(event:Event):void
{
var loaderInfo:LoaderInfo = LoaderInfo(event.target);
var bitmapData:BitmapData = new BitmapData(loaderInfo.width, loaderInfo.height, false, 0xFFFFFF);
bitmapData.draw(loaderInfo.loader);
// result: bitmapData
}Synchronous JPEG ByteArray to BitmapData can be found here
You may Leave a comment or Subscribe to Comments RSS or Trackback this entry.
4 comments so far
How to resize image (BitmapData) at Jozef Chúťka's blog January 8, 2010 14:31 […] If you are looking for transformation between BitmapData, Bitmap, ByteArray, read this article. […]
isaac ewing January 12, 2012 08:09 wow, this is perfect, thank you for sharing… all the other sites have the answer as sending the byte array to an image component and setting the source… which is great but doesn’t work when you need to separate the method calls from the actual implementation of the raw data (hints your extremely useful asynchronous help)!!! thanks for sharing!
lumen June 23, 2014 21:29 Thanks. This is a handy Swiss Army knife for theses classes
Raj November 27, 2014 20:23 GREAT RESOURCE ON FLEX
Leave a comment
Please be polite and on topic. Your e-mail will never be published.