PROWAREtech

articles » current » dot-net » alpha-compositing-algorithm

.NET: Alpha Compositing Algorithm

How to convert an image with an alpha channel to an image without an alpha channel; written in C#.
Choose Background Color

First, choose a background color to be used for the transparent pixels.

Apply Alpha Compositing Algorithm

The formula for alpha compositing a single pixel typically involves blending each of the RGB values of the foreground image with those of the background color based on the alpha value. The alpha value ranges from 0 (completely transparent) to 255 (completely opaque). If the alpha value ranges from 0 to 1, simply multiply it by 255.

Here it is in C# code:


float ConvertAlphaChannelToBackground(float desiredBackgroundColor, float foregroundColor, float alpha)
{
	return (foregroundColor * alpha / 255) + (desiredBackgroundColor * (255 - alpha) / 255);
}

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE