Exploring GDI+ : Using The Brush

by Amr Elsehemy 15. February 2008 08:01

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)

Tags: ,

GDI+

Comments

2/17/2008 5:44:01 AM #

Jason Kealey

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

Jason Kealey Canada |

2/17/2008 9:24:36 AM #

Amr

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.

Amr Egypt |

Comments are closed

About the author

Amr Elsehemy
MCSD C#.Net,
MCTS Sql 2005,
MCPD Enterprise
avatar
E-mail me Send mail

Calendar

<<  March 2010  >>
MoTuWeThFrSaSu
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar