使用任何注册点坐标绘制显示对象
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
具有阶段,如果不是,则可以将其添加到阶段然后再次移除。