views:

35

answers:

1

I am trying to take images I am placing in a flex canvas component to a bitmap. I was able to get to the point where I'm not getting an error but then no image shows up and the image I save out as a jpg is blank. I imagine I'm not setting the bitmap data correctly but can't figure out what I am doing wrong.

Here is the code where I am converting it to a bitmap:

var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(_renderPop);
   var imageByteArray:ByteArray = imageSnap.data as ByteArray;
   var bLoader:Loader = new Loader();
   bLoader.loadBytes(imageByteArray);

   var bmd:BitmapData = new BitmapData(500,500);
   bmd.draw(bLoader);

   var imgTest:Image = new Image();
   imgTest.source = bmd;
   _renderPop.renderCanvas.addChild(imgTest);



   var fileRef:FileReference = new FileReference();
   fileRef.save(bLoader, 'testImage.jpg');

_renderPop.renderCanvas is where I am placing the images. Anybody see anything wrong?

A: 

Hi pfunc

In your code:

var bLoader:Loader = new Loader();
bLoader.loadBytes(imageByteArray);

...you're assuming that the bytes are being loaded immediately; try putting an event listener on the loader as follows:

bLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);

private function completeHandler(event:Event):void
{
    // ... the rest of your code goes here
}

The loadBytes functions works like the load function in that they are both asynchronous processes so you'll need the event listener. It is a little counter-intuitive, and i've made the same mistake myself several times.

If that doesn't work, maybe leave out the contentLoaderInfo property, but the above should work...

Let me know if you come right :)

Danny Kopping
OK thanks, so that seems to get me a little farther. But now I am getting an invalid bitmapdata error. Do you see anything else that I might need to do to capture the bitmapdata correctly?
pfunc
Just to clarify, I am getting the invalid bitmapdata error on this line:var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(_renderPop.renderCanvas,150);If I just use _renderPop then I don't get an error, but I get a blank bitmapImage. _renderPop.renderCanvas is an mx:Canvas object. I can trace it out, it is there.
pfunc
What exactly is this ImageSnapshot class you're using? Instead of using that component, try doing it manually... There's a very interesting blog post on InsideRIA for doing just this (and a lot more!)http://insideria.com/2008/03/image-manipulation-in-flex.htmlDoes that help at all?
Danny Kopping
It does help a bit, I just went in an used a jpegEncoder. It is working better now, but the image I am getting is blank. So it must just be the bounds I am using to draw the bitmap.
pfunc
It could also be that your component that you're trying to draw hasn't rendered correctly yet. When are you triggering the BitmapData.draw function? It'd add a FlexEvent.CREATION_COMPLETE event on the component and then run the code... If this isn't the case, let me know
Danny Kopping