A brush is an object that holds a color, a picture, or a
drawing pattern and that is used to fill the interior of a closed shape.
This definition also means that there are various types of brushes with
different goals. To meet these goals, the .NET Framework provides support
for brushes in various namespaces with different classes. The parent of all
brushes is the Brush class defined in the System.Drawing
namespace.

Using a Brush

Because the main job of a brush is to fill a closed
shape, the Graphics class provides a method that corresponds to each
of the closed shapes we reviewed to draw in the previous lesson in order to
fill it. The methods are:

FillRectangle: Used to fill the interior of a rectangle or a
square

FillRectangle: Used to fill the interior of a series of
rectangles

FillEllipse: Used to fill the interior of an ellipse or a
circle

FillPolygon: Used to fill the interior of a polygon

FillClosedCurve: Used to fill the interior of a closed curve

FillPie: Used to fill the interior of a pie

FillPath: Used to fill the interior of a graphic path

To fill out a shape, call one of these methods, pass it
a brush value, followed by the location and dimensions of the shape. For
example, if you want to draw a rectangle and fill it with a brush, you would
use code similar to:

Like a pen, the primary characteristic of a brush is its
color. To help you create a simple brush, the System.Drawing
namespace provides the static sealed Brushes class. The only feature
this class provides is the ability to specify a color to use on a brush. As
a static class, you never have to instantiate it.

To create a simple brush whose only information is
provided by its color, call the Brushes class and access a color by
qualifying it with the name of the class. Each color is provided by its name
as a property. Here is an example of using the class:

The simplest type of brush is referred to as solid. This
type of brush is simply equipped with a color and it is used to fill a shape
with it. To get a solid brush, you use the SolidBrush class defined
in the System.Drawing namespace. It has only one constructor declared
with the following syntax:

public SolidBrush(Color color);

The color passed as argument must be a valid
definition of a Color. Here is an example:

If you plan to use different colors to fill different
shapes, you don't have to create a new brush for each shape. At any time,
before re-using the same brush previously defined, you can simply change its
Color. For this reason, the SolidBrush class is equipped with the
Color property. Here is an example of using it:

Like most objects used in graphics programming, a brush
consumes the computer resources. Therefore, after using it, you can free the
resources it was using by calling the Dispose() method. Here is an
example: