Home

GDI+: Solid Brushes

 

Introduction

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:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	SolidBrush brushBlue = new SolidBrush(Color.Blue);

	e.Graphics.FillRectangle(brushBlue, 20, 20, 200, 160);
}

Like all the other tools used in GDI+ (and GDI), including the other brushes we will review here, after you have defined a brush, every time you use it, the compiler refers to the object's previous characteristic(s) and applies it (them). 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:
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	SolidBrush colorizer = new SolidBrush(Color.Lime);
	Graphics graph = Graphics.FromHwnd(this.Handle);

	graph.FillRectangle(colorizer, 10, 10, 120, 120);
	colorizer.Color = Color.Salmon;
	graph.FillRectangle(colorizer, 140, 10, 120, 120);
	colorizer.Color = Color.Aqua;
	graph.FillRectangle(colorizer, 10, 140, 120, 120);
	colorizer.Color = Color.Navy;
	graph.FillRectangle(colorizer, 140, 140, 120, 120);
}

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:

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
	SolidBrush colorizer = new SolidBrush(Color.Lime);
	Graphics graph = Graphics.FromHwnd(this.Handle);

	graph.FillRectangle(colorizer, 10, 10, 120, 120);
	colorizer.Color = Color.Salmon;
	graph.FillRectangle(colorizer, 140, 10, 120, 120);
	colorizer.Color = Color.Aqua;
	graph.FillRectangle(colorizer, 10, 140, 120, 120);
	colorizer.Color = Color.Navy;
	graph.FillRectangle(colorizer, 140, 140, 120, 120);

	colorizer.Dispose();
}
 
 

Home Copyright © 2004-2010 FunctionX, Inc.