Why recalculate alpha value in ToBitmap() ext. method

May 28, 2010 at 4:27 AM

float alpha = pixels[offset * 4 + 3] / 255;

buffer[0] = (byte)(pixels[offset * 4 + 2] * alpha);  // blue
buffer[1] = (byte)(pixels[offset * 4 + 1] * alpha);  // green
buffer[2] = (byte)(pixels[offset * 4 + 0] * alpha);  // red
buffer[3] = (byte)(pixels[offset * 4 + 3] * alpha);  // alpha

as title, since the alpha value is given, why apply it again to each color?

Jun 2, 2010 at 11:01 AM

Found the answer at http://forums.silverlight.net/forums/p/115516/260441.aspx, because of the premultiplied ARGB32 value in WriteableBitmap.

There are two bugs in this method:

float alpha = pixels[offset * 4 + 3] / 255F; // append an 'f' at the end of 255

buffer[3] = pixels[offset * 4 + 3]; // should keep the A value

Coordinator
Jun 2, 2010 at 12:26 PM

Thanks for your help:

I am confused how it worked in the past, because I didnt have any problems with this method.

Jun 3, 2010 at 4:23 AM

Don't be confused, it is because that if the ALPHA value of your images are all 255, this method will work correctly. ^__^