Ballot Debris

What were we talking about? Who is John Galt?

My Links

Blog Stats

Article Categories

Archives

Post Categories

Blogroll

Using Alpha in .NET

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.

posted on Monday, January 30, 2006 4:12 PM

Feedback

No comments posted yet.
Title  
Name  
Url
Comments   
Please enter the following code into the box below to stop spammers

  
Enter Code Here *