📄 copyscreen.as
字号:
import flash.display.BitmapData;
import flash.geom.Rectangle;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
/*本类作用是将flash的舞台或舞台上的元件保存为图片;
CopyScreen(URLs:String,mc:MovieClip);构造函数 参数说明:mc你要存为图片的元件(可以为整个舞台),URLs服务器地址
startPrint(mc:MovieClip, x:Number, y:Number, w:Number, h:Number);此方法为开始执行保存操作
参数说明:mc你要存为图片的元件
x、y开始坐标默认为0
w、h宽度和高度
copyMove属性 用来设置存为图片的元件(可以为整个舞台)
URLstr属性 URLs服务器地址
sign属性 发送的数据前缀(可以不用设此属性)
用法举例:
将flash中名为mc的元件保存为jpg的图片到str上,
在舞台上新建一个元件命名为mc,一个名为testBox的文件;mc为你要存为图片的元件,testBox为显示进度,复制以下代码到第贞就ok;
还有重很要就是str服务器地址,就是你把图片保存在哪个服务器。接收端可以是php,asp,asp.net 都可以;
如果你不会写服务器请看本文档的最下面!(初学者注意:本类要和flash源文件放在同一目录)
var str:String="http://localhost/savePic.php"
var listener:Object = new Object();
listener.onProgress = function(target:MovieClip, loaded:Number, total:Number) {
textBox.text = "保存中...."+Math.round((loaded/total)*100);
};
listener.onComplete = function(target:MovieClip, load_var:LoadVars) {
textBox.text="完成";
};
testCopyScreen = new CopyScreen(str,_root.mc);
testCopyScreen.addListener(listener);
testCopyScreen.startPrint();
//--------------------------------------------------------------------------------*/
class CopyScreen {
/////构造函数,mc为要打印的元件,URLs为发送数到的数据
function CopyScreen(URLs:String,mc:MovieClip){
if(mc!=undefined){
_copyMove=mc;
}else{
_copyMove=_root;
}
if(URLs!=undefined && URLs!=""){
this.URLstr=URLs;
}
AsBroadcaster.initialize(this);
}
//开始执行保存操作
public function startPrint(mc:MovieClip, x:Number, y:Number, w:Number, h:Number){
if(mc==undefined){
mc=_copyMove
}
broadcastMessage("onStart", _root);
if(x == undefined) x = 0;
if(y == undefined) y = 0;
if(w == undefined)
{
w = mc._width;
if(mc==_root) w=Stage.width
}
if(h == undefined)
{
h = mc._height;
if(mc==_root) h=Stage.height
}
var bmp:BitmapData = new BitmapData(w, h, false);
record = new LoadVars();
record.width = w
record.height = h
record.cols = 0
record.rows = 0
var matrix = new Matrix();
matrix.translate(-x, -y);
bmp.draw(mc, matrix, new ColorTransform(), 1, new Rectangle(0, 0, w, h));
id = setInterval(getPixs, 5, this, _root, bmp, URLstr,sign);
trace("你正在打印"+mc)
}
///主体收集相素
private function getPixs(scope, movie, bit,URLstr,sign){
var str_pixel:String
var pixel:Number
var requestData:LoadVars = new LoadVars();
if(sign==undefined) sign="pix"
scope.record[sign + scope.record.rows] = new Array();
for(var a = 0; a < bit.width; a++){
pixel = bit.getPixel(a, scope.record.rows)
str_pixel = pixel.toString(16)
if(pixel == 0xFFFFFF) str_pixel = ""; // 不发送空白的像素
scope.record[sign + scope.record.rows].push(str_pixel)
}
///movie本对象,scope.record.rows已完成进度, bit.height总的进度
scope.broadcastMessage("onProgress", movie, scope.record.rows, bit.height) //发送状态信息事件
scope.record.rows += 1
if(scope.record.rows >= bit.height){
clearInterval(scope.id)
scope.broadcastMessage("onComplete", movie, scope.record) // 完成事件
////////////////////发送数据到服务器////////////////
System.security.allowDomain(URLstr);
bit.dispose();
trace(URLstr)
if(URLstr!="" && URLstr!=undefined){
try{
scope.record.sendAndLoad(URLstr,requestData,"POST");
}catch(e){
trace("出错:"+e)
}
}
}
}
public function set copyMove(s){
if(s!=undefined)
_copyMove=s
}
public function get copyMove(){
return _copyMove
}
public var addListener:Function
public var broadcastMessage:Function
private var id:Number;
public var sign:String="cxh";//发送的数据前缀
public var record:LoadVars;//要发送的数据
private var _copyMove:MovieClip;//要复制的元件
public var URLstr:String;//接收数据的服务器地址
}
/*
以下是PHP版本,同样用asp,asp.net 也可以,在这里我就不多说了
/////////////////////////将以下文本保存为.php格式即可////////////////////////
<?php
error_reporting(0);
$w = (int)$_POST['width'];
$h = (int)$_POST['height'];
$img = imagecreatetruecolor($w, $h);
imagefill($img, 0, 0, 0xFFFFFF);
$rows = 0;
$cols = 0;
for($rows = 0; $rows < $h; $rows++){
$c_row = explode(",", $_POST['cxh' . $rows]);
for($cols = 0; $cols < $w; $cols++){
$value = $c_row[$cols];
if($value != ""){
$hex = $value;
while(strlen($hex) < 6){
$hex = "0" . $hex;
}
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
$test = imagecolorallocate($img, $r, $g, $b);
imagesetpixel($img, $cols, $rows, $test);
}
}
}
imagejpeg($img, "cxh.jpg", 90);
?>
/////////////////////////////////////////////////////////////////////////
如果你不会搭php服务器请继续往下看(iis的搭建我就不在这里说了)
到其官方站点下载 php-5.2.1-Win32.zip 并解压
将PHP文件解压到一个目录下,目录地址为 "D:\web\php"。
将PHP目录(D:\web\php)下的php.ini-dist复制到windows目录下,并改名为php.ini。
打开php.ini,在记事本中查找“extension_dir”,然后把 extension_dir = "./" 修改为 extension_dir = "D:\web\php\ext",查找extension=php_mbstring.dll、extension=php_mysql.dll和extension=php_gd2.dll,extension=php_ming.dll,extension=php_zip.dll,extension=php_w32api.dll,然后把前面的“;”去掉。
复制 php5ts.dll和 libmysql.dll到 C:\WINDOWS\system32 中。
接下来在IIS中进行设置,打开默认网站-〉属性对话框,切换到“主目录”选项卡,然后点“配置”按钮打开“应用程序配置对话框”,点“添加”按钮打开“添加/编辑应用程序扩展名映射”对话框。在“可执行文件”输入框中,指向D:\web\php\php5isapi.dll,在“扩展名”输入框里面输入“.php”(注意:在windows XP下需要加“.”),并选中“脚本引擎”和“检查文件是否存在”两个复选框,一路确定并退出IIS。
然后把上面的代码保存为savePic.php在C:\inetpub\wwwroot目录下
//////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////非常感谢大家和我一起分享此类///////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
本类用于actionScript 2.0
Email:cxh612@tom.com
Msn:hb_cxh@hotmail.com
我的个人网站:http://www.hbcxh.cn (纯flash+asp和早期作品)
2007.11.16 11:16 写于长沙,希望和大家一起交流as技术;
///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -