Currently it is not posssible to create a screen capture application using AS 3.0 and AIR Beta 1.

I built a demo screen capture application to test the fact.

Here is the screenshot of the application:

scr
Image created and saved by the application:

ashwinee

Here is the code of the application I built to test the fact.


//code begin//
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”absolute” applicationComplete=”init();”>
<mx:Style>
Application
{
background-color: “”;
background-image: “”;
margin-top: 0;
margin-right: 0;
margin-bottom: 0;
margin-left: 0;
}
</mx:Style>

<mx:Script>
<![CDATA[

import mx.controls.Alert;

import flash.display.Bitmap;
import com.adobe.images.JPGEncoder;
import flash.filesystem.*;

private var bitmapData:BitmapData;
private var newImage:File;
private var fileStream:FileStream;

private function init():void {
fileStream = new FileStream();
stage.window.width = 500;
stage.window.height = 500;

}
private function snagPic():void {
bitmapData = new BitmapData(this.width,this.height,false,0x00000000);
bitmapData.draw(this,new Matrix());
var bitmap : Bitmap = new Bitmap(bitmapData);
var jpg:JPGEncoder = new JPGEncoder();
var ba:ByteArray = jpg.encode(bitmapData);
newImage = File.applicationStorageDirectory.resolve(“Images/” + fileName.text + “.jpg”);
fileStream = new FileStream();
fileStream.open(newImage, FileMode.UPDATE);
fileStream.writeBytes(ba);
}
private function onClose(evt:MouseEvent):void
{
stage.window.close();
}

]]>
</mx:Script>
<mx:Panel x=”50″ y=”37″ width=”250″ height=”200″ layout=”absolute”>
<mx:TextInput x=”35″ y=”87″ id=”fileName” text=”MyNewFile”/>
</mx:Panel>
<mx:Button x=”83″ y=”284″ label=”snap” click=”snagPic()”/>
<mx:Button x=”207.5″ y=”284″ label=”Close” click=”onClose(event)”/>

</mx:Application>

//code ends//

Note : Make the application transparent.

The application tries to save the image of the stage along with all the display object on it in the runtime.The stage is set to transparent.so while running the application we only see the display objects and not the stage.

But when the image is drawn on a BitmapData object the background color is set to white when transparent property is set to true in case of the property being set to false, the background color turns black.

Colin Moock also discussed about screen captures in Essential Actionscript 3.0 book. Here is an excerpt from the book:
26.7.1.2. No arbitrary screen captures
Note that it is not possible to take a screen capture of an arbitrary rectangular region of the
screen via ActionScript. ActionScript can only copy display objects to bitmap format.
ActionScript’s closest analog to screen capturing the display area is to use the Stage instance
as draw()’s source parameter, as in:
var canvas:BitmapData = new BitmapData(100, 100, false, 0xFFFFFFFF);
canvas.draw(someDisplayObject.stage);
where someDisplayObject is a DisplayObject instance on the display list. The preceding
code will produce a bitmap containing every object currently on the display list, with the
following caveats:
• The .swf file’s background color is not copied to the bitmap.
• If any objects on the display list are inaccessible due to security restrictions, they are not copied to the bitmap and a SecurityError exception is thrown.”

Lets hope that building such an application will be possible the release of AIR version 1.

Advertisements