第一种是BitmapData.draw..
适合任意显示对象,
第二种是BitmapData.copyPixels...
只用于BitmapData,
代码如下..
bmp为目标对象.请按开发环境自行替换~
01.package
02.{
03. import flash.display.Bitmap;
04. import flash.display.BitmapData;
05. import flash.display.DisplayObject;
06. import flash.display.Sprite;
07. import flash.geom.Matrix;
08. import flash.geom.Point;
09. import flash.geom.Rectangle;
10.
11. public class Intercept extends Sprite
12. {
13. [Embed(source="2009081009150339.jpg")]
14. private var BmpClass:Class;
15. public function Intercept()
16. {
17. //bmp为源显示对象
18. var bmp:Bitmap = new BmpClass() as Bitmap;
19. addChild(bmp);
20. bmp.y = 100;
21.
22. //方法一.BitmapData.draw
23. var bit1:BitmapData = draw(bmp,new Rectangle(150,150,100,100))
24. var bmp1:Bitmap = new Bitmap(bit1);
25. addChild(bmp1);
26.
27. //方法二:BitmapData.copyPixels
28. var bit2:BitmapData = copy(bmp.bitmapData,new Rectangle(150,150,100,100))
29. var bmp2:Bitmap = new Bitmap(bit2);
30. addChild(bmp2);
31. bmp2.x = 150;
32. }
33.
34.
35.
36. /**
37. * 以区域重绘显示对象
38. * @param target 显示对象
39. * @param rect 区域
40. * @return bitmapData
41. */
42. private function draw(target:DisplayObject,rect:Rectangle):BitmapData
43. {
44. var matrix:Matrix = new Matrix(1,0,0,1,-rect.x,-rect.y);
45. rect.x = rect.y = 0;
46. var bitmapData:BitmapData = new BitmapData(rect.width,rect.height);
47. bitmapData.draw(target,matrix,null,null,rect);
48. return bitmapData;
49. }
50.
51.
52. /**
53. * 以区域复制bitmapData;
54. * @param target 目标BitmapData
55. * @param rect 区域
56. * @return bitmapData
57. */
58. private function copy(target:BitmapData,rect:Rectangle):BitmapData
59. {
60. var bitmapData:BitmapData = new BitmapData(rect.width,rect.height);
61. bitmapData.copyPixels(target,rect,new Point(0,0));
62. return bitmapData;
63. }
64. }
65.}
文章来自: 闪客居(www.flashas.net) 详文参考:http://www.flashas.net/as/20090819/4432.html