Archive | March 2011

BitmapData draw() or copyPixels() transparency issue

Usually to copy BitmapData we use Draw() or copyPixels() to redraw one bitmap to another. You will realise that if your source bitmap is a transparent image it gets copied over with white patch around it instead of transparency.

var bitmapData:BitmapData = new BitmapData(sourceBitmap.width, sourceBitmap.height);
var bitmap:Bitmap = new Bitmap(bitmapData);
imgHolder.source = bitmap;

If you look in to documentation you will find out that BitmapData constructor takes transparent as third parameter and it is actually true by default. As in our example our new bitmap should have been already transparent but it wont get copied the way we expect. Key to this issue is actually the fourth parameter which is fillColor. You just have to set it with 0 alpha and bingo we should have our bitmap copied as transparent image as we need.

var bitmapData:BitmapData = new BitmapData(sourceBitmap.width, sourceBitmap.height, true, 0x00);

Another thing to note is that fourth parameter is a uint which take ARGB value, which mean the first two character represent Alpha and remaining 6 the actual color. So given that we can actually set any color for the transparent part of the image and even control alpha of it.

var bitmapData:BitmapData = new BitmapData(sourceBitmap.width, sourceBitmap.height, true, 0x33FF0000);

%d bloggers like this: