KSK
KSK

Reputation: 695

Actionscript 3: Converting bytearray to PNG and display on the scene


I'm getting getting a PNG image stored in SQL through a WCF get call. The image is encoded as a base64 string and delivered to my AS3 code. I need to extract the image from the data and show it on the scene.
Among other things, I have also tried this...

    var imgArray:ByteArray = Base64.decodeToByteArray(responseXML.ImageObject);
var myRect:Rectangle = new Rectangle(100,100,200,200);
var bmd:BitmapData = new BitmapData(200,200,true,0xAAAAAAAA);
bmd.setPixels(myRect, imgArray);
var image:Bitmap = new Bitmap(bmd,"auto",true);
this.addChild(image);

but to no avail.
HELP!

Upvotes: 1

Views: 5100

Answers (3)

KSK
KSK

Reputation: 695

I used DanielH's input and got the image to load on my stage. Here is what I did in the event handler function...

    function ImageLoaded(e:Event):void
    {
        var bmd:BitmapData = new BitmapData(imageLoader.ImageLoader.width,imageLoader.ImageLoader.height,true, 0xFFFFFF);
        bmd.draw(imageLoader.ImageLoader);
        var image:Bitmap = new Bitmap(bmd,"auto",true);
        image.width=40;
        image.height=40;
        if(!CheckAndStoreImageIDKey(imageLoader.ImageID))
        {
            Images[imageLoader.ImageID] = image;
        }
    }

Upvotes: 2

ansiart
ansiart

Reputation: 2591

why don't you use a loader and loadbytes? It's native.

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoad)
loader.loadbytes(byteArray);

private function handleLoad(e:Event):void {
  var loader:Loader = e.currentTarget as Loader;
  // removelistener,etc

  trace(loader.content as Bitmap);
}

The problem with your code is that PNG is compressed, bitmap is uncompressed.

Upvotes: 6

strah
strah

Reputation: 6742

Try PNGDecoder (http://www.ionsden.com/content/pngdecoder)

import ion.utils.png.PNGDecoder;

var bmd:BitmapData = PNGDecoder.decodeImage(imgArray);

Upvotes: 1

Related Questions