使用Flex中,所有的UI组件显示其最终的内部表示都是bitmapData,所以使用BitmapData对象可以对任何Flex2 UI组件进抓图,一下是范例代码,代码很简单,其精髓就是使用了BitmapData.draw(source:IBitmapDrawable, matrix:Matrix = null,…)方法,所有的FLEX2 UI组件都是DisplayObject的子类,而DisplayObject实现了IBitmapDrawable接口…
使用Flex中,所有的UI组件显示其最终的内部表示都是bitmapData,所以使用BitmapData对象可以对任何Flex2 UI组件进抓图,一下是范例代码,代码很简单,其精髓就是使用了BitmapData.draw(source:IBitmapDrawable, matrix:Matrix = null,…)方法,所有的FLEX2 UI组件都是DisplayObject的子类,而DisplayObject实现了IBitmapDrawable接口:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute">
<mx:Script>
<![CDATA[
import mx.core.UIComponent;
private function captureFullScreen() : void
{
var bd : BitmapData = getBitmapData( UIComponent( mx.core.Application.application ) );
targetImage.source = new Bitmap( bd );
}
private function captureHiddenDatagrid() : void
{
var bd : BitmapData = getBitmapData( UIComponent( hiddenDg ) );
targetImage.source = new Bitmap( bd );
}
/**********************************************************
This function is the real "meat" of this code snippet.
**********************************************************/
private function getBitmapData( target : UIComponent ) : BitmapData
{
var bd : BitmapData = new BitmapData( target.width, target.height );
var m : Matrix = new Matrix();
bd.draw( target, m );
return bd;
}
]]>
</mx:Script>
<mx:Button
id="captureButton"
label="Capture Full Screen"
click="captureFullScreen()" />
<mx:Button
id="captureButton2"
label="Capture Hidden Datagrid"
click="captureHiddenDatagrid()"
x="153"/>
<mx:Image
id="targetImage"
x="10"
y="30"/>
<mx:DataGrid
x="99"
y="64"
id="hiddenDg"
visible="false">
<mx:columns>
<mx:DataGridColumn headerText="Column 1" dataField="col1"/>
<mx:DataGridColumn headerText="Column 2" dataField="col2"/>
<mx:DataGridColumn headerText="Column 3" dataField="col3"/>
</mx:columns>
</mx:DataGrid>
</mx:Application>
来源:onflex.org
O comments at "Flex2:使用BitmapData抓图"