AS3 stage screen shot to hard drive save

青春壹個敷衍的年華 提交于 2019-12-08 11:54:13

问题


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!



来源:https://stackoverflow.com/questions/16091327/as3-stage-screen-shot-to-hard-drive-save

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!