GDI+ Pens and Fonts in VB.NET

Pens and fonts are two common and most used objects in GDI+. This tutorial explains how to use pens and fonts in your VB.NET applications.
  • 4094
The System.Drawinging.Pen and System.Drawing.Font classes represent pen and font objects in GDI+.

The Pen Class

A pen draws a line of specified width and style. You always use Pen constructor to create a pen. The constructor initializes a new instance of the Pen class. You can initialize it with a color or brush.

Initializes a new instance of the Pen class with the specified color. The Color is the only one parameter passed in constructor as an argument.

public Sub Pen(Color)

Initializes a new instance of the Pen class with the specified Brush. The Brush is the only one parameter passed in constructor as an argument.

Public Sub Pen(ByVal Brush)

Initializes a new instance of the Pen class with the specified Brush and width.

Public

Sub Pen(ByVal Brush, ByVal float)

Initializes a new instance of the Pen class with the specified Color and Width.

Public

Sub Pen(ByVal Color, ByVal float)

Here is one example:

Dim

pn As New Pen(Color.Blue)
or
Dim pn As New Pen(Color.Blue, 100)

Some of its most commonly used properties are Alignment, Brush, Color, and Width, which are self-explanatory.

The Font Class

The Font class defines a particular format for text such as font type, size, and style attributes. You use font constructor to create a font.

Initializes a new instance of the Font class with the specified attributes.

public Sub Font(string, float)

Initializes a new instance of the Font class from the specified existing Font and FontStyle.

Public

Sub Font(ByVal Font, ByVal FontStyle)

Where FontStyle is an enumeration, which include values Bold, Italic, Regular, StrikeOut, and Underline, which are self-explanatory. Here is one example:

Dim g As Graphics
Dim font As New Font("Times New Roman", 26)

Some of its most commonly used properties are:

 

Bold Gets a value indicating whether this Font is bold.
FontFamily Gets the FontFamily of this Font.
Height Gets the height of this Font.
Italic Gets a value indicating whether this Font is Italic.
Name Gets the face name of this Font.
Size Gets the size of this Font.
SizeInPoints Gets the size, in points, of this Font.
Strikeout Gets a value indicating whether this Font is strikeout (has a line through it).
Style Gets style information for this Font.
Underline Gets a value indicating whether this Font is underlined.
Unit Gets the unit of measure for this Font.

Working with Fonts and FontFamily

Although we've already discussed Fonts in the previous section of this chapter but there is more to be discussed about fonts.

The System.Drawing.Font class represents a font type. For example,

Dim greenSolid As New Font("Verdana", 14)

 

Creates a font type verdana with size 14. You can also use a FontStyle as an argument when constructing a font. The below line creates a font of Tahoma with different styles.

Dim

redStyle As New Font("Tahoma", 20, FontStyle.Bold|FontStyle.Italic|FontStyle.Underline);
g.DrawString("Text on the Screen", greenSolid,
new SolidBrush(Color.Green), 10,10) g.DrawString("Red Text", redStyle, new HatchBrush(HatchStyle.DiagonalCross, Color.Chocolate, Color.Red), 50,40)

The FontStyle Enumeration defines these styles.

Table 11-6. FontStyle Enumeration

 

Member Description
Bold Bold Text.
Italic Italic Text.
Regular Regular Text.
StrikeOut Text with a line in middle.
Underline Underline Text.


FontFamily is another class we'd like to discuss before moving to the next topic. System.Drawing.FontFamily class represents a font family, which is used to work with similar kind of fonts with different style variations. For example, Tahoma font can have different styles and size.

 

Dim

tahomaFmly As New FontFamily("Tahoma")

Here tahomaFmly represents the Tahoma font family.

In the below sample example, green28 and red14Italic uses same font family to create a Tahoma font with different styles.

Dim

fontFmly As New FontFamily("Tahoma")
Dim green28 As New Font(fontFmly, 28)
Dim red14Italic As New Font(fontFmly, 14, FontStyle.Italic)
g.DrawString("Text on the Screen", green28,
new SolidBrush(Color.Green), 10,10) g.DrawString("Text on the Screen", red14Italic, new SolidBrush(Color.Red), 10,10)

FontFamily class provides members to get information about a family of fonts. These members include GetName, GetLineSpacing, GetEmHeight, IsStyleAvailable and so on. All of them are self-explanatory.

Working with Pens

We've seen earlier in this chapter that the Graphics class's draw members such as DrawLine, DrawRectangle, DrawArc and so on use pens to draw objects. A pen draws a line with specified width and style. The System.Drawing.Pen and System.Drawing.Pens classes represent pens in GDI+. Some of the System.Drawing.Pen class properties are defined in the following table.

Public Instance Properties

 

Brush Attached brush with a pen.
Color Color of a pen.
Dash Style Dashed line style.
DashCap Style at the beginning and at the end of dashed lines.
DashedOffset Distance from the start of a line before a dashed pattern.
CustomStartCap,
CustomEndCap
Custom cap style at the beginning and end of the line.
DashPattern Dash pattern.
DashStyle Dashed line style.
StartCap,EndCap Starts and ends the cap style.
PenTyle Style of lines of a pen.
Transform Geometric transformation of a pen.
Width Width of a pen.

The System.Drawing.Pens class represents pens of all the colors. The System.Drawing.Pens class is non inheritable. For example,

 

Dim

redPn As New Pen(Color.Red, 14)
Dim redPen As Pen = Pens.Red
g.DrawLine(redPen, 10, 40, 50, 60)

You can set the other properties:

pnGreen.Width = 4
pnGreen.DashStyle = DashStyle.DashDot

The DashStyle members and their description are given below -

 

Member Description
Custom custom dash style.
Dash A line consisting of dashes.
DashDot A line of a repeating pattern of dash-dot.
DashDotDot A line of a repeating pattern of dash-dot-dot.
Dot A line consisting of dots.
Solid A solid line.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.