开发者

Flex 3 - Saving an image of a component

开发者 https://www.devze.com 2022-12-08 12:56 出处:网络
Im currently trying to work the imageSnapshot function in flex. Ive been looking hard but I cant seem to find a solution to my problem. I wish to take a screenshot of one of my components, to capture

Im currently trying to work the imageSnapshot function in flex. Ive been looking hard but I cant seem to find a solution to my problem. I wish to take a screenshot of one of my components, to capture the final output of my program, since a plain "printscreen" cuts off some of the output due to it scrolling. My current code looks like -

<mx:ApplicationControlBar dock="true">
    <mx:Button label="Take snapshot of Profile" 
        click="takeSnapshot();" />
</mx:ApplicationControlBar> 

Which when called does -

private function takeSnapshot(even:Event=null):void {
    var imageSnap:ImageSnapshot = ImageSnapshot.captureImage(viewstack1);

Now i think this is taking the image of the viewstack which I want... But Im stumped on what to do from here! Is it not possible to now just copy the image to the clipboard, or produce a new window in m开发者_如何学运维y browser with the entire image inside? If anyone has any other way to do this, suggestions would be wonderful.

Thank you for your time.


You could draw the viewstack into a BitmapData object and encode it to png using as3corelib. Something like:

var screenshotData : BitmapData = new BitmapData(viewstack1.width, viewstack1.height, true, 0x00000000);
screenshotData.draw(viewstack1);
var outputData:ByteArray = PNGEncoder.encode(screenshotData);
// Save outputData as a file to disk, send to webserver etc..

edit: Uhh, this probably confuses you even more. Sorry about that. ImageSnapshot has a property called data, which i'm guessing gives you pretty much the same result as this. You could save it as a file to disk using flash.net.FileReference


I built on Antti's example to make a working component (with full code), which you can view here. You can print pretty much any component you like (I've got an image and a datagrid there).

All you need to add to Antti's code is this:

var fr:FileReference = new FileReference();
var encoder:PNGEncoder = new PNGEncoder();

//Antti's code here

fr.save(outputData,"datagrid.jpg");

That will open up a file save dialog for you.

Cheers, Casp

0

精彩评论

暂无评论...
验证码 换一张
取 消