Home

GDI+: Introduction to Brushes

 

The Brush

 

Introduction

In the previous lesson, we drew two types of figures: line-based and closed shapes. These figures required a pen to draw their shape. The particularity with closed shapes is that they can be filled, with a color, a picture, or a pattern.

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.

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
  • 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:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	e.Graphics.FillRectangle(SomeBrush, 20, 20, 200, 160);
}

Over all, there are four types of brushes.

 

Home Copyright © 2004-2010 FunctionX, Inc.