BumbleShrimp
BumbleShrimp

Reputation: 2200

Preserving PNG transparency in BitmapData

I'm trying to store BitmapData from a MovieClip that is a PNG with transparency. When I copyPixels() the stored BitmapData to the stage bitmap's BitmapData, the transparency has been replaced with solid white.

I've been googling this for over an hour now and trying some things on my own.

Here is the BitmapData storing code:

//for all block types
for(var block_type_properties in this.block_types)
{


    //block_types[i][3] = bitmapdata
    this.block_types[block_type_properties].push(new BitmapData(this.block_size,this.block_size,true,0x000000));

    //block_types[i][4] = scale 
    this.block_types[block_type_properties].push(new Number(this.block_size/ this.block_types[block_type_properties][2].width));

    //block_types[i][5] = matrix
    this.block_types[block_type_properties].push(this.block_types[block_type_properties][2].transform.matrix);

    //scale the matrix
    this.block_types[block_type_properties][5].scale(this.block_types[block_type_properties][4],this.block_types[block_type_properties][4]);

    //apply the matrix to the movieclip
    this.block_types[block_type_properties][2].transform.matrix = this.block_types[block_type_properties][5];

    //draw the scaled movieclip into the bitmapdata
    this.block_types[block_type_properties][3].draw(this.block_types[block_type_properties][2],this.block_types[block_type_properties][5]);


}

and here is the part that actually pastes the pixels onto the stage's bitmap:

this.stage_background_bitmap_data.copyPixels(this.block_types[1][3],new Rectangle(0,0,this.block_size,this.block_size),new Point(blocks_array[block_count]['block'].x,blocks_array[block_count]['block'].y));

Does anyone have any idea why the transparency in the PNG is being replaced with white?

Edit:

These are two lines of code from the constructor:

this.stage_background_bitmap_data = new BitmapData(this.level_width,this.level_height,true,0x00000000);         
this.stage_background_bitmap = new Bitmap(this.stage_background_bitmap_data);

I've ruled out the matrix scaling from being the problem. I removed it and there was no effect on the transparency, or lack thereof.

Upvotes: 0

Views: 1638

Answers (1)

Michael Brewer-Davis
Michael Brewer-Davis

Reputation: 14276

Specify the mergeAlpha parameter. From the docs for BitmapData.copyPixels():

The mergeAlpha property controls whether or not the alpha channel is used when a transparent image is copied onto another transparent image. To copy pixels with the alpha channel data, set the mergeAlpha property to true. By default, the mergeAlpha property is false.

Upvotes: 4

Related Questions