Saving a Screen Capture to a JPEG Image File in Actionscript 3.0

The program below demostrates how to capture an image of the stage and save it as a jpg image. We have drawn a yellow circle give the image capture something more interesting to render. To save the jpg image, just left-click the box above and select a location to save the image in.

The program for this jpg image capture is shown below. Additionally, you will need the JPGEncoder file which you can get here: https://github.com/mikechambers/as3corelib. To use the source file, select Edit->Preferences->Actionscript->Actionscript 3.0 Settings . . . and add the folder where you put “mikechambers-as3corelib-release.93-8-g24c6c16\mikechambers-as3corelib-24c6c16\src” to the src paths. Then you should be able to compile and execute the Actionscript code.

In this program, we import the JPGEncoder class and then draw a filled yellow circle on the stage. Next, we register an event listener to call the function SaveJPG() to save the image when a left-click occurs; Actionscript requires that file-saving code be contained in a user-initiated function.

Inside the callback, we create a BitmapData object that is the size of our stage and draw the stage into it. Then we create the encoder and call encode() to put the jpg data into a ByteArray. Finally, we create a FileReference object and save the jpg data.

This entry was posted
on Wednesday, February 29th, 2012 at 2:58 am and is filed under Actionscript.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

3 Responses to “Saving a Screen Capture to a JPEG Image File in Actionscript 3.0”

I’ve been searching for this for weeks! Thank you so much!
I’m actually working on a Cake Designer app, and downloading the final design to be downloaded as JPEG.
Will be posting the progress soon on my website: thetotobox dot com. Cheers!