Exploring GDI+ : Using The Brush

Brush HierarchyOne of the core classes in the GDI+ framework built in .Net is the Brush class. There are 5 types of Brushes in .Net that help you to color your objects by lot of variations the five types are as shown in the figure :



The 5 types as shown are :

SolidBrush Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths.
HatchBrush Defines a rectangular brush with a hatch style, a foreground color, and a background color.
LinearGradientBrush Encapsulates a Brush with a linear gradient.
PathGradientBrush Encapsulates a Brush object that fills the interior of a GraphicsPath object with a gradient.
TextureBrush Each property of the TextureBrush class is a Brush object that uses an image to fill the interior of a shape.

I have built a simple application that uses and shows the output of each brush, remember that options of each bursh is not limited to my sample, there are much more one can do using these brushes.

solid hatch linearGradient

pathGradient texture

Sample Application Code :

Exploring GDI+ Brushes.zip (13.24 kb)







2 responses to “Exploring GDI+ : Using The Brush”

  1. Jason Kealey Avatar

    Although it doesn’t really matter in your application, I see you’re not Disposing the brushes. Could this lead to a memory leak?

  2. Amr Avatar

    Actually, you are right, Brushes are objects that cause memory leaks and need to be disposed.
    They should be used like this :

    using(SolidBrush brush = new SolidBrush(Color.Yellow))
    { /* use the brush */ }

    Also, for using known Color solid brushes there is a static class named Brushes which has some predefined brushes that can be used as an alternative to not get in the headache of clearing the memory >> which is Brushes.Red
    In big applications that should be taken care of.
    Thanks for pointing out this.

Leave a Reply

Your email address will not be published. Required fields are marked *