Uli
Uli

Reputation: 2693

AS3 - ByteArray to bitmap

I encoded an image to a ByteArray. How to convert it to a bitmap from that ByteArray? Thanks. Uli

Upvotes: 1

Views: 11364

Answers (5)

Panzercrisis
Panzercrisis

Reputation: 4750

[Bindable]
private static var m_barrLogo:ByteArray;
.
.
.
private function init_m_barrLogo():void
{
    m_barrLogo = someValue;

    // The Image below will automatically read the new value of the ByteArray and
    // display the appropriate image.
}
.
.
.
<mx:Image source="{m_barrLogo}" y="10" x="10" scaleContent="true" height="140"
        width="145"/>

Upvotes: 0

Lex
Lex

Reputation: 5014

I did this test below which works well. It converts a Bytearray to Bitmap. The format of the image is irrelevant. png, jpg, gif all hold their format in the meta data which the bitmapdata class will interpret for you. You should use JPEGEncoder or PNGEncoder when saving.

        import mx.events.FlexEvent;

        public var _file:File;
        public var _stream:FileStream;
        public var _load:Loader;

        protected function init(event:FlexEvent):void {
            _file = File.desktopDirectory;
            _file.addEventListener( Event.SELECT, selectComplete);
            _file.browseForOpen( 'Image' );
        }
        private function selectComplete( event:Event ):void {
            _stream = new FileStream();
            _stream.addEventListener( Event.COMPLETE, loadComplete );
            _stream.openAsync( _file, FileMode.READ );
        }
        private function loadComplete( event:Event ):void {
            var ba:ByteArray = new ByteArray();
            _stream.readBytes( ba );
            _load = new Loader();
            _load.contentLoaderInfo.addEventListener( Event.COMPLETE, loadbytesComplete );
            _load.loadBytes( ba );
        }
        private function loadbytesComplete( event:Event ):void {
            var bit:Bitmap = _load.content as Bitmap;
            img.source = bit;
        }

Upvotes: 2

Marcx
Marcx

Reputation: 6836

Use Loader

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.display.BitmapData;
 
private function startLoad():void
{
  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
}

Upvotes: 0

laurent
laurent

Reputation: 90863

What is the format of your ByteArray? If the pixels are in 32-bit ARGB format, you can create a new BitmapData object and set the pixels using BitmapData.setPixels(). Then just assign the bitmap data to a bitmap object.

If they are not in 32-bit ARGB format, you will probably need to convert them first.

Upvotes: 1

Related Questions