views:

199

answers:

3

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.

+1  A: 

You 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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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.

George Profenza
The tutorial you linked to helped me a lot. Many thanks!
picardo
+2  A: 

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.

A3CoreLibs on Google Code

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.

Sephiroth AMFPHP Tutorial

Do I have to send the ByteArray to the server in order to save it as a file? What about using FileReference.save()?
picardo
Never mind. I just realized that FileReference.save() only works in Flash 10.
picardo
+2  A: 

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()

Cay