Bits & Bytes

Posts Tagged ‘flash’

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: 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.

import com.adobe.images.JPGEncoder;

// Draw a circle on the staqe
graphics.drawCircle(160, 120, 100);

stage.addEventListener(MouseEvent.CLICK, SaveJPG);

function SaveJPG(e:MouseEvent):void {
	// Store the image capture of stage in a BitmapData Object
	var qImageData:BitmapData = new BitmapData(320, 240);
	// Set the encoding to high-quality
	var qEncoder:JPGEncoder = new JPGEncoder(100);
	var qBytes:ByteArray = qEncoder.encode(qImageData);
	var qFile:FileReference = new FileReference();, "YellowCircle.jpg");

Loading an Image Into a Bitmap in Actionscript 3.0

The code below shows how to load a remote image and store it in a Bitmap object (this works for local images too). We begin by creating a Loader object, setting the callback function OnImageLoad() to get called when the image is done loading, and then calling the load() function to load the image. The OnImageLoad() function casts the loaded content to a Bitmap, where the BitmapData is used to create a new Bitmap that is added to the stage for display.

const ksXoaXLogo:String = "";

var qLoader:Loader = new Loader();
qLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnImageLoad);
qLoader.load(new URLRequest(ksXoaXLogo));
function OnImageLoad(e:Event):void {
    var qTempBitmap:Bitmap 	= qLoader.content as Bitmap;
    var qBitmap:Bitmap		= new Bitmap(qTempBitmap.bitmapData);

Responding to Mouse Click Events in Actionscript 3.0

To respond to mouse events of any kind, we create a callback function that takes a MouseEvent and returns void. Then we call addEventListener() with the event and the callback function. Below, we show how this is done for MOUSE_DOWN and MOUSE_UP events, but the same method can be used for all of the mouse event types.

Inside the callback functions, we change the message that is displayed by the TextField and position the message to be displayed where the event occurred. If you click in the box above, you will see the message change and move as you press and release the left mouse button. In this example, we position the message using the stage coordinates. However, we can also use the members localX and localY to get the position based on the object that is listening for the events.

var qMessage:TextField = new TextField();
qMessage.text = "Click In Here";

stage.addEventListener(MouseEvent.MOUSE_DOWN, OnPress);
function OnPress(e:MouseEvent): void {
	qMessage.text = "Down";
	qMessage.x = e.stageX;
	qMessage.y = e.stageY;

stage.addEventListener(MouseEvent.MOUSE_UP, OnRelease);
function OnRelease(e:MouseEvent): void {
	qMessage.text = "Up";
	qMessage.x = e.stageX;
	qMessage.y = e.stageY;