开发者

Trim whitespace around a transparent PNG

So, I've managed to get myself stuck in a situation where a database full of images ( transparent images of various products ) needs to be placed on the stage, all of which need to be aligned the same by the products height.

My problem is that the png's of products are 'floating' and I have no control of where about's in the png it will sit ( could be tight at the top and loads of room at the bottom, vice versa)

Does anyone know an existing way to find out the png's 'true' height ( width is an extra ). I've thought about lo开发者_如何学运维oping through the bitmap data and checking but wondered if anyone has invented this wheel already?

eg Example with room at top / Example with none, really


you can use the method of the BitmapData class getColorBoundsRect() to get the rectangle of non-transparent content. The documentation gives this example too:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html

Thanks, Alistair


As Alistair says, the getColorBoundsRect will ultimately be your best option.

I haven't looked into it too much, but I'm not sure if getColorBoundsRect allows you to "select all non 100% alpha-ed pixels". If it doesn't you can easily use the BitmapData.threshold method to get you to that stage.

I'd do something like duplicate the bitmap, run the threshold method on it turning all non alpha-ed pixels bright green, then run getColorBoundsRect selecting all the green pixels you just created.


The solution I eventually came to was the below, most likely not the most performant way but it works.

    /**
     * Cuts off the transparency around a bitmap, returning the true width and height whilst retaining transparency
     *  
     * @param input Bitmap
     * 
     */
    private function trimTransparency(input:BitmapData,colourChecker:uint = 0x00FF00):Bitmap {

        //Keep a copy of the original
        var orignal:Bitmap = new Bitmap(input);

        //Clone the orignal with a white background
        var clone:BitmapData = new BitmapData(orignal.width, orignal.height,true,colourChecker);
        clone.draw(orignal);

        //Grab the bounds of the clone checking against white
        var bounds:Rectangle = clone.getColorBoundsRect(colourChecker, colourChecker, false);

        //Create a new bitmap to return the changed bitmap
        var returnedBitmap:Bitmap = new Bitmap();
        returnedBitmap.bitmapData = new BitmapData(bounds.width, bounds.height,true,0x00000000);
        returnedBitmap.bitmapData.copyPixels(orignal.bitmapData,bounds, new Point(0,0));    
        return returnedBitmap;


    }


This is a solution I came up with, in case anyone needs:

    public static function trimAlpha(source:BitmapData):BitmapData {
        var notAlphaBounds:Rectangle = source.getColorBoundsRect(0xFF000000, 0x00000000, false);
        var trimed:BitmapData = new BitmapData(notAlphaBounds.width, notAlphaBounds.height, true, 0x00000000);
        trimed.copyPixels(source, notAlphaBounds, new Point());
        return trimed;  
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜