如何从BitmapData中剪切一个Shape?

aeg*_*esi 2 flash actionscript bitmap actionscript-3 bitmapdata

我有一个填充的 Shape,和一个与Shape的边界框宽度和高度相同的BitmapData.
我需要从BitmapData中剪切Shape(基本上将BitmapData绘制到形状上......)[如此:http://imgur.com/uwE5F.png ]

我使用相当hackish方法:

        public static function cutPoly(img:BitmapData, s:Shape, bounds:Bounds):BitmapData {
        var temp:BitmapData = new BitmapData(bounds.width, bounds.height, true);
        Main.inst.stageQuality("low"); //hack to kill anti-aliasing
        temp.draw(s,new Matrix());
        Main.inst.stageQuality("high"); // end hack

        //0xFF00FF00 is the color of the shape
        makeColTrans(temp,0xFF00FF00); //makes the color transparent :P
        //return temp;
        img.draw(temp);
        //img.draw(temp);
        temp.dispose();
        makeColTrans(img, 0xFFFFFFFF);
        return img;
    }
Run Code Online (Sandbox Code Playgroud)

我想知道是否有更好的方法......一个不仅仅是一个黑客.

erk*_*ene 5

它也可以被认为是hack但你可以在容器精灵中添加位图和(绘制)形状,用形状掩盖位图并再次绘制结果图像.您获得的唯一好处是使用运行时的原生绘图算法,只有当您的makeColTrans逐个像素地扫描整个位图时才会出现这种情况.

编辑代码示例:

    public static function cutPoly(sourceBitmapData:BitmapData, maskShape:Shape, bounds:Rectangle):BitmapData {
        // you might not need this, supplying just the sourceBitmap to finalBitmapData.draw(), it should be tested though.
        var sourceBitmapContainer:Sprite = new Sprite();
        sourceBitmapContainer.addChild(sourceBitmap);
        sourceBitmapContainer.addChild(maskShape);

        var sourceBitmap:Bitmap = new Bitmap(sourceBitmapData);
        maskShape.x = bounds.x;
        maskShape.y = bounds.y;
        sourceBitmap.mask = maskShape;

        var finalBitmapData:BitmapData = new BitmapData(bounds.width, bounds.height, true, 0x00ffffff);
        // or var finalBitmapData:Bitmap = new BitmapData(maskShape.width, maskShape.height); not too sure about the contents of the bounds...
        finalBitmapData.draw(sourceBitmapContainer);

        return finalBitmapData;
    }
Run Code Online (Sandbox Code Playgroud)