Easy Way to Learn ASP.NET

GDI+ Tutorial

GDI+ Introduction
GDI+ Lines
GDI+ Rectangles
GDI+ Ellipses
GDI+ Arcs
GDI+ Polygons
GDI+ Cardinal Splines
GDI+ Bezier Splines
GDI+ Graphics Paths
GDI+ Brushes
GDI+ Regions
GDI+ Antialiasing
GDI+ Bitmaps
GDI+ Metafiles
GDI+ Transformations
GDI+ Text

Brushes and Filled Shapes in GDI+

We can draw a closed shape, such as a rectangle or an ellipse, is filled with a brush. GDI+ provides several brush classes for filling the interiors of closed shapes: SolidBrush, HatchBrush, TextureBrush, LinearGradientBrush, and PathGradientBrush.


Solid Brushes

To fill a closed shape, you need an instance of the Graphics class and a Brush. The instance of the Graphics class provides methods, such as FillRectangle and FillEllipse, and the Brush stores attributes of the fill, such as color and pattern. The Brush is passed as one of the arguments to the fill method.

The following code example shows how to fill a rectangle with a solid color red:

 


Hatch Brushes

We can fill a closed shape with a hatch brush. To fill a closed shape with a hatch brush, we need to create a HatchBrush object. To draw a closed shape call the fill method of the Graphics object, such as FillEllipse or FillRectangle.

The following code example shows how to fill a rectangle with a hatch brush:

 


Texture Brushes

With a texture brush, you can fill a shape with a pattern stored in a bitmap.

To fill a shape with a texture brush, we need to create a TextureBrush object. Then to draw a filled shape, call the fill method of the Graphics object.

The following code example shows how to fill a rectangle with a texture brush:

 


Gradient Brushes

GDI+ provides two kinds of gradient brushes: linear and path. You can use a linear gradient brush to fill a shape with color that changes gradually as you move across the shape horizontally, vertically, or diagonally.

A path gradient brush can be configured to change color as you move from the center of a shape toward the edge.

The following code example shows how fill a shape with a linear gradient brush:

 

The following code example shows how to fill a shape with a path gradient brush: