3

I have a problem involving loading transparent pngs. What I am trying to do, is to copy the alpha channel from a loaded png, and then apply this alpha channel to another Bitmapdata object.

Can anyone suggest how I would do this?

Bachalo
  • 6,965
  • 27
  • 95
  • 189

1 Answers1

6

As it turns out, I have a class that does exactly that:

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BitmapDataChannel;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    public class BitmapAlphaMerge
    {
        public static function merge (imgBitmap:Bitmap, maskBitmap:Bitmap) : Bitmap
        {
            var img:BitmapData = imgBitmap.bitmapData;
            var mask:BitmapData = maskBitmap.bitmapData;
            var mergeBmp:BitmapData = new BitmapData(img.width, img.height, true, 0);
            var rect:Rectangle = new Rectangle(0, 0, img.width, img.height);
            mergeBmp.copyPixels(img, rect, new Point());
            mergeBmp.copyChannel(mask, new Rectangle(0, 0, img.width, img.height), new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);
            return new Bitmap(mergeBmp);
        }
    }
}

The first parameter imgBitmap is the image you want to add the alpha channel to. maskBitmap is the Bitmap you are copying the channel from. You could modify this to use pure BitmapData objects quite easily.

Josh at The Nerdery
  • 1,397
  • 9
  • 16