AS3 stage screen shot to hard drive save

2019-06-09 09:25发布

问题:

I'm working on a drag and drop Flash game, after you place all the movable pieces I want to have a button that allows a person to save a screen shot of the entire stage.

I searched different samples and cant get this code to work.

Here is the AS3

snap_btn.addEventListener(MouseEvent.CLICK, snapShot );

function snapShot( evt:MouseEvent ):void
{

import com.adobe.images.JPGEncoder;

var jpgSource:BitmapData = new BitmapData (stage.width, stage.height); 
jpgSource.draw(stage); 



var jpgEncoder:JPGEncoder = new JPGEncoder(85); 
var jpgStream:ByteArray = jpgEncoder.encode(jpgSource);

var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream"); 
var jpgURLRequest:URLRequest = new URLRequest("jpg_encoder_download.php?name=sketch.jpg"); 
jpgURLRequest.requestHeaders.push(header); 
jpgURLRequest.method = URLRequestMethod.POST; 
jpgURLRequest.data = jpgStream; 
navigateToURL(jpgURLRequest, "_blank");


}

The PHP

<?php
if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) 
{ 
// get bytearray 
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"]; 

// add headers for download dialog-box 
header('Content-Type: image/jpeg'); 
header("Content-Disposition: attachment; filename=".$_GET['name']); 
echo $jpg; 
}
?>

I ended up getting this to work, I got rid of the php

snap_btn.addEventListener(MouseEvent.CLICK, snapShot );

function snapShot( evt:MouseEvent ):void
{

import com.adobe.images.JPGEncoder; 



var jpgSource:BitmapData = new BitmapData(stage.width, stage.height);
jpgSource.draw(stage);
var jpgEncoder:JPGEncoder = new JPGEncoder(80);
var imgByteData:ByteArray = jpgEncoder.encode(jpgSource);
file = new FileReference();

file.save(imgByteData, "Haven_Big_Dream_Map.jpg");




}

回答1:

I'll just rewrite answer of Evan.

Use FileReference to save file after using JPEGEncode or PNGEncode from adobe.corelib on BitmapData. Remember that older versions of FlashPlayer (<10) won't save files on drive!