Export (0) Print
Expand All

Pens, Brushes, and Colors

Visual Studio .NET 2003

You use pen and brush objects to render graphics, text, and images with GDI+. A pen is an instance of the Pen class, and is used to draw lines and outlined shapes. A brush is an instance of any class that derives from the MustInherit (abstract) Brush class, and can be used to fill shapes or paint text. Color objects are instances of classes that represent a particular color, and can be used by pens and brushes to indicate the color of rendered graphics. The following examples demonstrate the use of these objects.

' Visual Basic
' Creates a pen that draws in red.
Dim myPen as New Pen(Color.Red)
' Creates a brush that fills in solid blue.
Dim myBrush as New SolidBrush(Color.Blue)

// C#
// Creates a pen that draws in red.
Pen myPen = new Pen(Color.Red);
// Creates a brush that fills in solid blue.
SolidBrush myBrush = new SolidBrush(Color.Blue);

// C++
// Creates a pen that draws in red.
Pen* myPen = new Pen(Color::Red);
// Creates a brush that fills in solid blue.
SolidBrush* myBrush = new SolidBrush(Color::Blue);

The following sections discuss pens, brushes, and colors in further detail.

Pens

A pen is used to draw lines, curves, and to outline shapes. The following example shows how to create a basic black pen:

' Visual Basic
' This line creates a black pen with a default thickness of 1.
Dim myPen as New Pen(Color.Black)
' This line creates a black pen with a thickness of 5.
Dim myPen as New Pen(Color.Black, 5)

// C#
// This line creates a black pen with a default thickness of 1.
Pen myPen = new Pen(Color.Black);
// This line creates a black pen with a thickness of 5.
Pen myPen = new Pen(Color.Black, 5);

// C++
// This line creates a black pen with a default thickness of 1.
Pen* myPen1 = new Pen(Color::Black);
// This line creates a black pen with a thickness of 5.
Pen* myPen2 = new Pen(Color::Black, 5);

You can also create a pen from an already existing brush object. The following demonstrates how to create a pen that is based on an already existing brush called myBrush.

' Visual Basic
' Creates a pen with the same display properties as myBrush and a default 
' thickness of 1.
Dim myBrush As New SolidBrush(Color.Red)
Dim myPen as New Pen(myBrush)
' Creates a pen with the same display properties as myBrush and a 
' thickness of 5.
Dim myPen as New Pen(myBrush, 5)

// C#
// Creates a pen with the same display properties as myBrush and a default
// thickness of 1.
SolidBrush myBrush = new SolidBrush(Color.Red);
Pen myPen = new Pen(myBrush);
// Creates a pen with the same display properties as myBrush and a 
// thickness of 5.
Pen myPen = new Pen(myBrush, 5);

// C++
// Creates a pen with the same display properties as myBrush and a default
// thickness of 1.
SolidBrush* myBrush = new SolidBrush(Color::Red);
Pen* myPen1 = new Pen(myBrush);
// Creates a pen with the same display properties as myBrush and a 
// thickness of 5.
Pen* myPen2 = new Pen(myBrush, 5);

Once you have created a pen, you can then use it to draw a line, arc, or outlined shape. The following example demonstrates how to use a pen to draw an ellipse:

' Visual Basic
Dim myPen as New Pen(Color.Black)
Dim g as Graphics = Me.CreateGraphics
g.DrawEllipse(myPen, 20, 30, 10, 50)

// C#
Pen myPen = new Pen(Color.Black);
Graphics g = this.CreateGraphics();
g.DrawEllipse(myPen, 20, 30, 10, 50);

// C++
Pen* myPen = new Pen(Color::Black);
Graphics* g = this->CreateGraphics();
g->DrawEllipse(myPen, 20, 30, 10, 50);

Once your pen exists, you can change various properties that affect the way it presents lines. Properties such as Width and Color affect the appearance of the line, and the StartCap and EndCap properties allow you to add preset or custom shapes to the beginning or end of your lines. The DashStyle property allows you to choose between solid, dotted, dashed, or custom dashed lines, and the DashCap property allows you to customize the ends of the dashes in your lines. For more information on how to use pens to draw lines and shapes, see Drawing Lines and Shapes with GDI+.

Brushes

Brushes are objects that are used with a Graphics object to create solid shapes and to render text. There are several different types of brushes.

Brush ClassDescription
SolidBrushThe simplest form of brush, which paints in a solid color.
HatchBrushSimilar to a SolidBrush, but it allows you to select from a large variety of preset patterns to paint with, rather than a solid color.
TextureBrushPaints using a texture, such as an image.
LinearGradientBrushPaints two colors blended along a gradient.
PathGradientBrushPaints using a complex gradient of blended colors, based on a unique path defined by the developer.

All of these classes inherit from the Brush class, which is an abstract (MustInherit) class and cannot be instantiated.

Solid Color

The following example shows how to draw a solid red ellipse on your form. The ellipse will conform to the size of the rectangle provided for it (in this case, the ClientRectangle which represents the entire form).

' Visual Basic
Dim g as Graphics = Me.CreateGraphics
Dim myBrush as New SolidBrush(Color.Red)
g.FillEllipse(myBrush, ClientRectangle)

// C#
Graphics g = this.CreateGraphics();
SolidBrush myBrush = new SolidBrush(Color.Red);
g.FillEllipse(myBrush, ClientRectangle);

// C++
Graphics* g = this->CreateGraphics();
SolidBrush* myBrush = new SolidBrush(Color::Red);
g->FillEllipse(myBrush, ClientRectangle);

Patterns

Simple Patterns

A HatchBrush allows you to select from a large variety of preset patterns to paint with, rather than a solid color. The following example shows how to create a HatchBrush that paints with a plaid pattern, using red as the forecolor and blue as the backcolor.

' Visual Basic
Dim aHatchBrush as New _
   System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D. _
   HatchStyle.Plaid, Color.Red, Color.Blue)

// C#
System.Drawing.Drawing2D.HatchBrush aHatchBrush = new 
   System.Drawing.Drawing2D.HatchBrush(System.Drawing.
   Drawing2D.HatchStyle.Plaid, Color.Red, Color.Blue);

// C++
System::Drawing::Drawing2D::HatchBrush* aHatchBrush = new 
   System::Drawing::Drawing2D::HatchBrush(System::Drawing::
   Drawing2D::HatchStyle::Plaid, Color::Red, Color::Blue);

Complex Patterns

A texture brush fills shapes or text using an image as the pattern. The following example shows how to create a TextureBrush that paints using an image called myBitmap.

' Visual Basic
Dim myBrush as New TextureBrush(New Bitmap _
   ("C:\Documents and Settings\Joe\Pics\myPic.bmp"))

// C#
TextureBrush myBrush = new TextureBrush(new Bitmap(@"C:\Documents and 
   Settings\Joe\Pics\myPic.bmp "));

// C++
TextureBrush* myBrush = new TextureBrush(new Bitmap(
   "C:\\Documents and Settings\\Joe\\Pics\\myPic.bmp"));

Complex Shading

The gradient brushes provide support for complex shading. Using a LinearGradientBrush, you can create a smooth, progressive blending of two colors along a linear gradient. The PathGradientBrush supports many more complex shading and coloration options. The following example shows how to paint the form in a gradient that gradually blends from red to yellow.

' Visual Basic
Dim g As Graphics = Me.CreateGraphics
Dim myBrush As New System.Drawing.Drawing2D.LinearGradientBrush _
   (ClientRectangle, Color.Red, Color.Yellow, _
   System.Drawing.Drawing2D.LinearGradientMode.Vertical)
g.FillRectangle(myBrush, ClientRectangle)

// C#
Graphics g = this.CreateGraphics();
System.Drawing.Drawing2D.LinearGradientBrush myBrush = new 
   System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, 
   Color.Red, Color.Yellow, System.Drawing.Drawing2D.
   LinearGradientMode.Vertical);
g.FillRectangle(myBrush, ClientRectangle);

// C++
Graphics* g = this->CreateGraphics();
System::Drawing::Drawing2D::LinearGradientBrush* myBrush = new 
   System::Drawing::Drawing2D::LinearGradientBrush(ClientRectangle, 
   Color::Red, Color::Yellow, System::Drawing::Drawing2D::
   LinearGradientMode::Vertical);
g->FillRectangle(myBrush, ClientRectangle);

Colors

The .NET Framework structure Color is used to represent different colors. Colors are used with pens and brushes to specify the color to be rendered.

System-Defined Colors

There are several system-defined colors that can be accessed through the Color structure. Examples of these colors include:

' Visual Basic
Dim myColor as Color
myColor = Color.Red
myColor = Color.Aquamarine
myColor = Color.LightGoldenrodYellow
myColor = Color.PapayaWhip
myColor = Color.Tomato

// C#
Color myColor;
myColor = Color.Red;
myColor = Color.Aquamarine;
myColor = Color.LightGoldenrodYellow;
myColor = Color.PapayaWhip;
myColor = Color.Tomato;

// C++
Color myColor;
myColor = Color::Red;
myColor = Color::Aquamarine;
myColor = Color::LightGoldenrodYellow;
myColor = Color::PapayaWhip;
myColor = Color::Tomato;

Each of the above statements assigns myColor to a system-defined color of the indicated name.

User-Defined Colors

You can also create user-defined colors using the Color.FromArgb Method. This method allows you to specify the strength of the individual red, blue, and green components of a color.

' Visual Basic
Dim myColor as Color
myColor = Color.FromArgb(23,56,78)

// C#
Color myColor;
myColor = Color.FromArgb(23,56,78);

// C++
Color myColor;
myColor = Color::FromArgb(23,56,78);

This example generates a user-defined color that is roughly bluish-gray. Each number must be an integer ranging from 0 to 255, where 0 is an absence of that color and 255 is complete saturation of the specified color. Thus, Color.FromArgb(0,0,0) renders black, and Color.FromArgb(255,255,255) renders white.

Alpha Blending (Transparency)

You can also specify an alpha component with this method. Alpha represents transparency to objects behind the rendered graphics. Alpha-blended colors can be useful for a variety of shading and transparency affects. If you want to specify an alpha component, it should be the first of four arguments in the Color.FromArgb method, and requires an integer ranging from 0 to 255.

' Visual Basic
Dim myColor as Color
myColor = Color.FromArgb(127, 23, 56, 78)

// C#
Color myColor;
myColor = Color.FromArgb(127, 23, 56, 78);

// C++
Color myColor;
myColor = Color::FromArgb(127, 23, 56, 78);

This example creates a color that is bluish-gray and roughly 50% transparent.

You can also create an alpha-blended color by specifying an alpha component and a previously defined color.

' Visual Basic
Dim myColor as Color
myColor = Color.FromArgb(128, Color.Tomato)

// C#
Color myColor;
myColor = Color.FromArgb(128, Color.Tomato);

// C++
Color myColor;
myColor = Color::FromArgb(128, Color::Tomato);

This example creates a color that is approximately 50% transparent and the color of the system-defined color Tomato.

See Also

GDI+ Example Topics | GDI+ Graphics | Drawing Lines and Shapes with GDI+ | Drawing Text With GDI+ | Creating Graphical Images with GDI+

Show:
© 2014 Microsoft