Does anyone know how to take a screencap and download it to desktop in AS3? I know there is a great BitmapDataExporter in AS2 by Mario Klingenman but it doesn't work in AS3.
views:
199answers:
3You can try this:
var bitmapData:BitmapData = new BitmapData(stage.stageWidth,stage.stageHeight,false,0x000000);
bitmapData.draw(workspace);
var byteArray:ByteArray = bitmapData;
var request:URLRequest = new URLRequest ( 'yourserver/save.php' );
var loader: URLLoader = new URLLoader();
request.contentType = 'application/octet-stream';
request.method = URLRequestMethod.POST;
request.data = byteArray;
loader.load( request );
//and save.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>save</title>
</head>
<body>
<?php
$fp = fopen( 'file.txt', 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
echo "result: " + $fp;
?>
</body>
</html>
Also, you can use Adobe's corelib which has a JPEG encoder, and there are loads of great tutorials out there.
You can draw any DisplayObject in a BitmapData object using the draw method. However you cannot draw the stage or the root application class (security error), so you must contain your application inside a master Sprite that is then added to this stage.
public function createSnapShot(displayObject:DisplayObject):BitmapData
{
var nWidth:Number = displayObject.width;
var nHeight:Number = displayObject.height;
var bmd:BitmapData = new BitmapData(nWidth, nHeight, true, 0x00000000);
bmd.draw(displayObject);
return bmd;
}
Once you have your BitmapData you need to serialize it to a ByteArray so it can be sent using an URLLoader. To Serialize you either need the JpegEncoder which is included with the Flex SDK or you can use the encoder provided with adobe's core libraries.
In this example AMFPHP is used to round trip saving bitmapdata to a server and back agaib, it should serve as a good jumping off point for you, but you could also look into send multi-part form data.
In addition to the other answers, note that Flash Player 10 can directly save a file on the user's HD, without the need to send it to the server... see FileReference.save()