Search code examples
actionscript-3screenshotbitmapdata

How to take a screencap of a SWF and download it to user's desktop with AS3?


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.


Solution

  • 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">
    <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.