Flex/actionscript snapshot

dtivmk
dtivmk used Ask the Experts™
on
In the attached code :
after saving the file, when I try to open it, I get "This is not a valid bitmap file". I have tried 2-3 different viewers.
var bmd:BitmapData = ImageSnapshot.captureBitmapData(someSprite);
         trace("bmd size "+getSize(bmd));
         var bounds:Rectangle = new Rectangle(0, 0, bmd.width, bmd.height);
     var snapshot:ImageSnapshot = new ImageSnapshot(0,0,bmd.getPixels(bounds));

         //var snapshot:ImageSnapshot = ImageSnapshot.captureImage(someSprite);
     var file:FileReference = new FileReference();
     file.save(snapshot.data,'abc.png');

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
Hmm you should use captureImage and not captureBitmapData to do this. I don't know why you commented that but it's the correct way.

var snapshot:ImageSnapshot = ImageSnapshot.captureImage(someSprite);
var file:FileReference = new FileReference();
file.save(snapshot.data,'abc.png');

You should use this code to save the image instead.

Author

Commented:
with captureimage I cannot specify the scaling matrix or the clipping rectangle. That's why I am trying to use capturebitmapdata.
Top Expert 2009

Commented:
Oh. With capturebitmapdata you need a png encoder.

Take a look at http://code.google.com/p/as3corelib/.
Success in ‘20 With a Profitable Pricing Strategy

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
It doesn't seem to have any documentation.
Could you provide me with a link?
Also, 1 or 2 lines of sample code would also help.
Top Expert 2009
Commented:
Here's how you do it.

import com.adobe.images.PNGEncoder;
var bmd:BitmapData = ImageSnapshot.captureBitmapData(someSprite);
var img:ByteArray = PNGEncoder.encode(bmd);
var file:FileReference = new FileReference();
file.save(img,'abc.png');


Author

Commented:
The attached code allows you to draw only the portion inside the clip rectangle.
 In my case I didn't have to take into account a scaling matrix,
 even though I was using zoom In/Out features.
sw = someSprite.stage.stageWidth;			 
sh = someSprite.stage.stageHeight;			 
var cr:Rectangle = new Rectangle(x,y,cw,ch);//this clip rectangle should not overshoot your stage
//cr is the clip rectangle
var bmp:BitmapData = new BitmapData(sw,sh);
bmp.draw(someSprite,null,null,null,cr);

var bmp1:BitmapData = new BitmapData(cw,ch);
bmp1.copyPixels(bmp,cr,new Point(0,0));
var enc:JPEGEncoder = new JPEGEncoder();
var data:ByteArray = encoder.encode(bmd1);
new FileReference().save(data,'image.jpeg');

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial