//將stage上之顯示元件轉成.PNG檔案,並存於本機端,包含stage也可轉存
<?xml version="1.0" encoding="utf-8"?>
<mx:Application applicationComplete="init()" xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.graphics.codec.PNGEncoder;
private var enable:Boolean;
private function init():void
{
enable = true;
}
private function byDown(e:MouseEvent):void
{
if(!enable)return;
var _bitMapData:BitmapData;
//第三參數,true,支援32Bit透明度
_bitMapData = new BitmapData(cvs.width , cvs.height , true ,0x000000);
_bitMapData.draw(cvs);
//利用PNGEncoder將_bitMapData編碼成PNG,ByenArray
var b:ByteArray = new PNGEncoder().encode(_bitMapData);
//利用FileRefrence將轉ByetArray存成PNG檔於本機上
var fr:FileReference = new FileReference();
fr.save(b, "Filename-" + new Date().time.toString() + ".png" );
}
]]>
</mx:Script>
<mx:VBox>
<mx:Button mouseDown="byDown(event)"/>
<mx:Canvas id= "cvs" backgroundColor="#00ff00" width="100" height="100" alpha="0.6">
<mx:Button id= "btn" width="50" height="50" label="測試" x="25" y="25"/>
</mx:Canvas>
</mx:VBox>
</mx:Application>
沒有留言:
張貼留言