使用任何註冊點座標繪製顯示物件
public function drawDisplayObjectUsingBounds(source:DisplayObject):BitmapData {
var bitmapData:BitmapData;//declare a BitmapData
var bounds:Rectangle = source.getBounds(source);//get the source object actual size
//round bounds to integer pixel values (to aviod 1px stripes left off)
bounds = new Rectangle(Math.floor(bounds.x), Math.floor(bounds.y), Math.ceil(bounds.width), Math.ceil(bounds.height));
//to avoid Invalid BitmapData error which occures if width or height is 0
//(ArgumentError: Error #2015)
if((bounds.width>0) && (bounds.height>0)){
//create a BitmapData
bitmapData = new BitmapData(bounds.width, bounds.height, true, 0x00000000);
var matrix:Matrix = new Matrix();//create a transform matrix
//translate if to fit the upper-left corner of the source
matrix.translate(-bounds.x, -bounds.y);
bitmapData.draw(source, matrix);//draw the source
return bitmapData;//return the result (exit point)
}
//if no result is created - return an empty BitmapData
return new BitmapData(1, 1, true, 0x00000000);
}
附註:對於 getBounds()
返回有效值,物件必須至少具有一次階段訪問許可權,否則值為偽造。可以新增程式碼以確保傳遞的 source
具有階段,如果不是,則可以將其新增到階段然後再次移除。