1

Is it possible to get the bitmap data from a component using ActionScript?

I dynamically load an image. onComplete I create a Flex Image component and add the loaded image to the source

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void 
{
     var image:Image = new Image();
     image.x = 0;
     image.y = 0;
     image.source = e.currentTarget.content;
     canvas.addChild(image); // canvas is already added as an MXML element.
 }

Later I want to create a new Image component and get the bitmapData from the first Image.

I have tried this

canvas.getChildAt(0)

Which seems to give me the Image, but I can not figure out how to get the bitmap data.

canvas.getChildAt(0).bitmapData; 

gives me a compile error "... undefined property"

Does anyone know how ot get the bitmap data so I can use it in my new Image component?

Thanks in advance,

Ran

Leon Gaban
  • 36,509
  • 115
  • 332
  • 529
Ran
  • 2,061
  • 2
  • 15
  • 10

3 Answers3

2

Check out ImageSnapshot.captureBitmapData()

http://livedocs.adobe.com/flex/3/langref/mx/graphics/ImageSnapshot.html

cliff.meyers
  • 17,666
  • 5
  • 51
  • 66
  • Thanks, the captureBitmapData did it! Do you know if it is possible to capture the bitmap without any applied filters? – Ran Jan 18 '10 at 23:21
2

Cliff's answer will give you a screenshot of the Image; to get the underlying BitmapData for the image without doing a screenshot, you can try

 Bitmap(image.content).bitmapData

This should avoid any filters as well.

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
0

This should do it.

var bd:BitmapData = new BitmapData(myComponent.width, myComponent.height, true, 0);
bd.draw(myComponent);
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Sophistifunk
  • 4,742
  • 4
  • 28
  • 37