Brushes, Pens and Alpha Blending in GDI+

This article has been excerpted from book "Graphics Programming with GDI+".

In GDI+, every color is a combination of ARGB components; each of the alpha, red, green, and blue components is represented by 8 bits. The alpha component in a color structure represents the transparency of the color, which varies from 0 to 255. The value 0 represents full transparency, and 255 represents full opacity.

The final color of an ARGB color structure is calculated by the following formula:

            Final Color = (Source Color x alpha /255) + [Background Color x (255-alpha)/255]

This formula is applied on each component of the source color and background color.

In alpha blending, an application creates a color with an alpha component and uses this color to create a pen or a brush. This pen or brush is used to draw a fill graphics shapes, and it calculates the final color. Alpha blending may sound unfamiliar, but programmatically it is simply a method of setting the alpha component (transparency) of a color, and using it to fill and draw graphics shapes.

Brushes, Pens and Alpha Blending

The process of alpha blending involves three simple steps. First an application creates a color with transparency (the alpha component). The following line creates a Color object with alpha component value 40:

            Color clr = Color.FromArgb(40, 255, 255, 255);

The second step is to create a brush or pen using that color. The following lines create a transparent pen and a brush:

            Pen transPen = new Pen(clr, 10);
            SolidBrush semiTransBrush = new SolidBrush(clr);

Finally, the application uses the transparent brush or pen to fill and draw graphics shapes, lines and curves. The following code uses the Pen and Brush objects we created in the previous steps to draw a line and to draw and fill rectangle:

            g.DrawLine(transPen, 10, 30, 200, 30);
            g.FillRectangle(semiTransBrush, rect);

Listing 9.31 uses this approach to draw lines, a rectangle, an ellipse, and text object with varying transparency. You can add this code to a menu item or a button click event handler.

LISTING 9.31: Using alpha blending to draw non-opaque or semi-opaque graphics shapes


    public partial class Form1 : Form
        public Form1()

        private void Form1_Paint(object sender, PaintEventArgs e)
            Graphics g = this.CreateGraphics();

            // Create pens with semitransparent colors
            Rectangle rect = new Rectangle(220, 30, 100, 50);
            Pen transPen =   new Pen(Color.FromArgb(128, 255, 255, 255), 10);
            Pen totTransPen =  new Pen(Color.FromArgb(40, 0, 255, 0), 10);

            // Draw line, rectangle, ellipse, and string using semitransparent colored pens
            g.DrawLine(transPen, 10, 30, 200, 30);
            g.DrawLine(totTransPen, 10, 50, 200, 50);
            g.FillRectangle(new SolidBrush(Color.FromArgb(40, 0, 0, 255)), rect);
            rect.Y += 60;
            g.FillEllipse(new SolidBrush(Color.FromArgb(20, 255, 255, 0)), rect);
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(90, 0, 50, 255));
            g.DrawString("Some Photo \nDate: 04/09/2001",
                new Font("Verdana", 14), semiTransBrush,
                new RectangleF(20, 100, 300, 100));

            // Dispose of object

Figure 9.42 shows the output from Listing 9.31. The lines rectangle, ellipse, and text on this form are semitransparent.


FIGURE 9.42: Drawing semitransparent graphics shapes


Hope the article would have helped you in understanding Brushes, Pens and Alpha Blending in GDI+. Read other articles on GDI+ on the website.

This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Similar Articles
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.