【问题标题】:AS3 stage screen shot to hard drive saveAS3阶段截图到硬盘保存
【发布时间】:2013-04-12 01:29:36
【问题描述】:

我正在开发一个拖放式 Flash 游戏,在你放置所有可移动的部分后,我想要一个按钮,让人们可以保存整个舞台的屏幕截图。

我搜索了不同的示例,但无法让此代码正常工作。

这里是 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");


}

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; 
}
?>

我最终得到了这个工作,我摆脱了 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");




}

【问题讨论】:

  • 我对 PHP 并不太熟悉,但看起来您正在使用它来保存文件。为什么不使用FileReference 直接通过Flash 为用户提供保存对话框?无法帮助您使用 JPG 编码,但如果是由于 PHP 的原因,您绝对可以使用 FileReference 来做到这一点
  • 酷谢谢你的提示,我得到了它来使用这个代码 'snap_btn.addEventListener(MouseEvent.CLICK, snapShot);函数 snapShot(evt:MouseEvent):void { import com.adobe.images.JPGEncoder; var jpgSource:BitmapData = new BitmapData(stage.width, stage.height); jpgSource.draw(舞台); var jpgEncoder:JPGEncoder = new JPGEncoder(80); var imgByteData:ByteArray = jpgEncoder.encode(jpgSource);文件 = 新文件参考(); file.save(imgByteData, "imagename.jpg"); }'
  • 请注意,所有导入都应在类声明之外或在 CS5 中的代码顶部完成。在函数内导入是不好的做法,可能会导致问题
  • 非常感谢您的提示。我正在慢慢学习。

标签: actionscript-3 flash screenshot hard-drive


【解决方案1】:

我将重写 Evan 的答案。

在 BitmapData 上使用 adobe.corelib 中的 JPEGEncode 或 PNGEncode 后,使用 FileReference 保存文件。请记住,旧版本的 FlashPlayer (

【讨论】:

    猜你喜欢
    • 2015-05-16
    • 1970-01-01
    • 2011-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2012-11-17
    相关资源
    最近更新 更多