How to: Draw with Opaque and Semitransparent Brushes
When you fill a shape, you must pass a Brush object to one of the fill methods of the Graphics class. The one parameter of the SolidBrush constructor is a Color object. To fill an opaque shape, set the alpha component of the color to 255. To fill a semitransparent shape, set the alpha component to any value from 1 through 254.
When you fill a semitransparent shape, the color of the shape is blended with the colors of the background. The alpha component specifies how the shape and background colors are mixed; alpha values near 0 place more weight on the background colors, and alpha values near 255 place more weight on the shape color.
Example
The following example draws a bitmap and then fills three ellipses that overlap the bitmap. The first ellipse uses an alpha component of 255, so it is opaque. The second and third ellipses use an alpha component of 128, so they are semitransparent; you can see the background image through the ellipses. The call that sets the CompositingQuality property causes the blending for the third ellipse to be done in conjunction with gamma correction.
The following illustration shows the output of the following code.
Dim bitmap As New Bitmap("Texture1.jpg")
e.Graphics.DrawImage(bitmap, 50, 50, bitmap.Width, bitmap.Height)
Dim opaqueBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim semiTransBrush As New SolidBrush(Color.FromArgb(128, 0, 0, 255))
e.Graphics.FillEllipse(opaqueBrush, 35, 45, 45, 30)
e.Graphics.FillEllipse(semiTransBrush, 86, 45, 45, 30)
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected
e.Graphics.FillEllipse(semiTransBrush, 40, 90, 86, 30)
Bitmap bitmap = new Bitmap("Texture1.jpg");
e.Graphics.DrawImage(bitmap, 50, 50, bitmap.Width, bitmap.Height);
SolidBrush opaqueBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 0, 0, 255));
e.Graphics.FillEllipse(opaqueBrush, 35, 45, 45, 30);
e.Graphics.FillEllipse(semiTransBrush, 86, 45, 45, 30);
e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;
e.Graphics.FillEllipse(semiTransBrush, 40, 90, 86, 30);
Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of PaintEventHandler.
See Also
Tasks
How to: Give Your Control a Transparent Background
How to: Draw Opaque and Semitransparent Lines
How to: Create Transparent Windows Forms