Matt Murrell
Matt Murrell

Reputation: 2351

AS3: Duplicating a PNG image causes loss of transparency

I am using LoaderMax to load an external PNG and display it in many places so I use the following code to duplicate the image:

var cd:ContentDisplay = ContentDisplay(loader.getContent("name"));
var b1 = Bitmap(cd.rawContent);
var old = b1.bitmapData;
var bmp = new Bitmap(b1);

container.addChild(bmp);

The original image has a 50% on the alpha channel, but when I create the new bitmap from from the same bitmapData object, it does not preserve the alpha channel.

If I try to copy the alpha channel (see the code below; if I understand correctly, the alpha channel is copied from itself, to itself)- the transparency is on the new image , but the code throws an error...

bmp.copyChannel(old, new Rectangle(0, 0, old.width, old.height), new Point(), BitmapDataChannel.ALPHA, BitmapDataChannel.ALPHA);

Error:

ReferenceError: Error #1069: Property copyChannel not found on flash.display.Bitmap and there is no default value.
    at barmask/frame1()

How can I duplicate a PNG and maintain the alpha transparency... preferably without the error?

P.s. Please forgive any obvious mistakes, I am a ActionScript Newb...

Upvotes: 0

Views: 1357

Answers (3)

Denis Kartashevskiy
Denis Kartashevskiy

Reputation: 1

Bitmap doesn't have a copyChannel method as the error says :)

Instead of this:

bmp.copyChannel(...

You need to do this:

bmp.bitmapData.copyChannel(...

Upvotes: 0

James Tomasino
James Tomasino

Reputation: 3581

His answer was very close to complete:

new BitmapData(w, h, true, 0); 

The last property ensuring that flash doesn't include a background in the new image. That should solve your issue. It has to be exactly "0", not 0x000000, either.

Upvotes: 2

www0z0k
www0z0k

Reputation: 4434

create your BitmapData instance, passing 3 parameters to the constructor: new BitmapData(w, h, true), Boolean value is transparency
also check the transparent property of the source BitmapData

Upvotes: 0

Related Questions