I was asked again today how to draw "translucent" stuff in .NET (GDI+). My response was "Use the
Alpha channel." Noting that this was not the first time I had been asked about this, I thought a brief explanation here was appropriate. From Wikipedia:
The value of alpha in the color code ranges from 0.0 to 1.0, where 0.0 represents a fully transparent color, and 1.0 represents a fully opaque color.
In .NET the range of the value is in byte form (0-255), with 255 representing 1.0. Thus there are 256 alpha channel settings. When drawing, one can adjust the value of the alpha channel to allow other colors to show through.
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(255,Color.Yellow)), new Rectangle(50,50,100,100));
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(150,Color.Blue)), new Rectangle(0,0,100,100));
}
This simple example draws two boxes in a picturebox. The point at which they intersect makes a kind of green. This is due to the alpha channel setting of 150 on the blue box. This is a simplistic overview since alpha channels can be used for dialogs, images and a few other things. Watch for more on alpha channels in the future.