Working with Brushes and Pens in GDI+


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

Brushes and pens are the two most frequently used objects in graphics application. Pens are used to draw the outlines of graphics objects such as lines and curves; brushes are used to fill the graphics objects' interior areas (e.g., filling a rectangle or an ellipse). In this article we will discuss how to create and use various types of brushes and pens.

We begin by discussing brushes, brush types, their methods and properties, and how to create and use them in GDI+.

GDI+ provides the Pen and Pens classes to represent pens. In this article we will discuss how to create different kinds of pens using the Pen class and its properties, and how to use the Pen class methods. We will also discuss how to add line caps, dash caps, line dash styles, and line cap styles. In Section 4.3 and 4.4 we will discuss the transformation of pens and brushes.

The SytemPens and SystemBrushes classes represent the system pens and brushes, respectively. In Section 4.5 we will discuss how to use these classes to work with system pens and brushes.

Understanding and Using Brushes

In the .NET Framework library, brush-related functionality is defined in two namespaces: System.Drawing and System.Drawing.Drafwing2D. The System.Drawing namespace defines general brush-related classes and functionality, and the System.Drawing.Drawing2D namespace defines advanced 2D brush-related functionality. For example the Brush, SolidBrush, TextureBrush, and Brushes classes are defined in the System.Drawing namespace; and the HatchBrush and GradientBrush classes are defined in the System.Drawing.Drawing2D namespace.

Before using brushes, obviously you must include the corresponding namespace to your application. Alternatively, you can use the namespace as a prefix to the class; for example, System.Drawing.Brush represents the Brush class if you do not wish to include the System.Drawing namespace in your application.

The code snippet in Listing 4.1 creates a red SolidBrush object and uses it to draw a rectangle. This code is written on a form's paint event handler. The first line gets the Graphics object of the form, and the second line creates a brush using the SolidBrush class, which later is used to fill a rectangle. The last line disposes of the SolidBrush object.

LISTING 4.1: Creating a solid brush


        Graphics g = e.Graphics;
        SolidBrush redBrush = new SolidBrush (Color.Red);
        Rectangle rect = new Rectangle (150, 80, 200, 140);
        g.FillRectangle(redBrush, rect);
        redBrush.Dispose();


The Brush Class

In the .NET Framework library, the Brush class is an abstract base class, which means you cannot create an instance of it without using its derived classes. All usable classes are inherited from the abstract Brush class. Figure 4.1 shows all the Brush-derived classes that can be used in your GDI+ application.

Applications generally call fill methods of the appropriate Graphics class, which in turn use brushes to fill GDI+ objects (such as an ellipse, an arch or a polygon) with a certain kind of brush. GDI+ provides four different kinds of brushes: solid, hatch, texture, and gradient. Figure 4.2 shows the brush types and their classes.

Figure 4.1.jpg

FIGURE 4.1: Classes inherited from the Brush class

Figure 4.2.jpg

FIGURE 4.2: Brush types and their classes.

The Brushes Class

The Brushes class is a sealed class (it cannot be inherited). Brushes provide more than 140 static members (properties), and each of these members represents a brush with a particular color (including all the standard colors). For instance, the Brushes.Pink, Brushes.Red and Brushes.Green members represents Brush object with the colors pink, red, and green respectively.

Solid Brushes

A Solid brush is a brush that fills an area with a single solid color. We create a SolidBrush object by calling its constructor and passing a Color structure as the only parameter. The Color structure represents a color. It has a static property for every possible color. For example, Color.Red represents the color red. The code snippet in Listing 4.2 creates three SolidBrush object with three different colors: red, green, and blue.

LISTING 4.2: Creating a SolidBrush object


          SolidBrush redBrush = new SolidBrush(Color.Red);
          SolidBrush greenBrush = new greenBrush(Color.Red);
          SolidBrush blueBrush = new blueBrush(Color.Red);


SolidBrush has only one property of interest: Color, which represents the color of the brush.

Listing 4.3 uses red, green, and blue solid brushes and fills an ellipse, a pie, and a rectangle using the FillEllipse, FillPie, and FillRectangle methods of the Graphics class, respectively.

LISTING 4.3: Using SolidBrush class


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
Brush
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            //Create three SolidBrush objects
            //Using the colors red, green and blue
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush greenBrush = new SolidBrush(Color.Green);
            SolidBrush blueBrush = new SolidBrush(Color.Blue);

            //Fill ellipse using red brush
            g.FillEllipse(redBrush, 20, 40, 100, 120);

            //Fill rectangle using blue brush
            Rectangle rect = new Rectangle(150, 80, 200, 140);
            g.FillRectangle(blueBrush, rect);

            //fill pie using green brush
            g.FillPie(greenBrush, 40, 20, 200, 40, 0.0f, 60.0f);

            //Dispose of objects
            redBrush.Dispose();
            greenBrush.Dispose();
            blueBrush.Dispose();
        }
    }
}


The output of Listing 4.3 draws an ellipse, a rectangle and a pie, as Figure 4.3 shows.

Fig4.3.gif

FIGURE 4.3 Graphics objects filled by SolidBrush

Conclusion

Hope the article would have helped you in understanding working with brushes and pens 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.


Similar Articles
Mindcracker
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.