|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Draw with Opaque and Semitransparent Brushes
When you fill a shape, you must pass aobject to one of the fill methods of the class. The one parameter of the constructor is a 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.
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 theproperty 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.
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 e, which is a parameter of .