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


using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;

namespace
Alpha_Blending_in_GDI_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

            // 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
            g.Dispose();
        }
    }
}


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

FIGURE-9.42.gif

FIGURE 9.42: Drawing semitransparent graphics shapes

Conclusion

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

bookGDI.jpg
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.

Up Next
    Ebook Download
    View all
    Learn
    View all