Translucent Image

I would like to know how to determine the color of a translucent image when it is on top of another image. To better explain it, for example, let say I have 2 images A and B and B is a translucent image and is on top of A. I would like to know the RGB values of the overlapping area of image B.

I know I can use GDI+ to create translucent images and it will do the rest for me. But that's not what I want, I would like to know the way to determine the color.

Thanks in advance.

[495 byte] By [WonWon] at [2007-12-25]
# 1

You need to Call GetPixel(int x, int y) method on a Bitmap to get the color at a specific X and Y coordinates, See Here:

Bitmap myBitmap = new Bitmap("hello.jpg");

Color pixelColor = myBitmap.GetPixel(2, 2);

pixelColor will tell you what color is at 2 and 2 co-ordinates!!!

Cheers ;-)

RizwanSharp at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...
# 2

The easiest way is to draw the images and get the resulting color, as suggested. If instead you want calculate the result without drawing, I think the formula is something as follows:

Assume we have two pixels, one with components a, r, g, b and we want to overlap another with components A, R, G, B.

The result should be something like:

Alpha = A + (int)((256 - A) / 256 * a));
Red = (int)(R*A / Alpha) + (int)(r*(Alpha - A)/Alpha);
Green = (int)(G*A/Alpha) + (int)(g*(Alpha - A)/Alpha);
Blue = (int)(B*A/Alpha) + (int)(b*(Alpha - A)/Alpha);

I extrapolated this formula by trial and error, so it might be not perfect, but it worked for all the tests I made.

(Please optimize the formula when you implement it!)

HTH
--mc

MarioCossi at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# General...