VB.NET 1.1 Tutorial - GDI+ - Part 1Previously we have written two programs that draw directly onto the form itself. The first program displayed Hello Mum by providing our own paint event. The second program also displayed Hello Mum by overriding the OnPaint method. These both used methods in the Graphics class to draw on the form. The Graphics class is part of the classes for drawing which are called GDI+. The term GDI stands for Graphical Device Interface. GDI+ is next evolution of GDI for the .Net Platform. All GDI+ classes reside in the following namespaces:
The Graphics ClassAs stated previously, the Graphics class is part of the System.Drawing namespace. It encapsulates a GDI+ drawing surface, and it is this surface that we draw upon. In the previous sections, Hello Mum - Windows Style - Part 3 and Hello Mum - Windows Style - Part 4, we used two methods of drawing the same text on the form's graphics surface. The first method (part 3) overrode the OnPaint method, Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) Dim g As Graphics = e.Graphics while the second method (part 4) provided a paint event. Private Sub Form1_Paint( ByVal sender As Object, ByVal e As PaintEventArgs ) _
Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Both of these use the same method to get a reference to the Graphics class. This is our surface to draw on. In order to draw on the surface we need to include a reference to the System.Drawing.dll. Imports System.Drawing The code we used to draw our string used one of the methods within this Graphics class. ' Method under System.Drawing.Graphics
e.Graphics.DrawString( "Hello Mum!", _
New Font("Verdana", 20), _
New SolidBrush(Color.Tomato), _
40, _
40)
The table below details some of the drawing methods available within the Graphics class.
Graphics ObjectsNotice in the above table that some of the methods refer to a Brush object. This is one of a three special items which are worthy of note. These three objects can use up resources, so they need to be managed within your program, making sure that you dispose of them when you are finished.
The Font ObjectThe Font class defines a particular format for text. This includes the font type, its size in points, and/or style attributes. The two most common constructors used to create fonts are: Public Sub New( String, Single ) where String is the name of the font you wish to use, and the Single is the size of the font in points. Public Sub New( Font, FontStyle ) where a new instance of the Font class is created from the specified existing Font and FontStyle. FontStyle is an enumeration with the following members:
For more information, follow the References section at the bottom of the page. The Brush ObjectThis is an abstract base class and cannot be instantiated. To create a brush object, we must use classes derived from Brush. These are
The SolidBrush class defines a brush made up of a single color. Brushes are used to fill graphics shapes such as rectangles, ellipses, pies, polygons, and paths. The Pen ObjectA Pen object draws a line of specified width and style. The line drawn by a Pen object can be filled in a variety of fill styles, including solid colors and textures. The fill style depends on brush or texture that is used as the fill object. A pen has four different types of constructor. Creates a new instance of the Pen class with the specified Color. Public Sub New( Color ) Creates a new instance of the Pen class with the specified Brush. Public Sub New( Brush ) Creates a new instance of the Pen class with the specified Brush and width. Public Sub New( Brush, Single ) Creates a new instance of the Pen class with the specified Color and width. Public Sub New( Color, Single ) The most common used properties are:
Disposing of ObjectsWhen you are finished with a Graphics object, you should call its Dispose method to ensure that all resources used by the Graphics object are freed. Simply letting an object variable go out of scope doesn't ensure that the resources used by the object are freed. Graphics objects can use considerable resources, so you should always call Dispose when you're finished with any Graphics object. If we re-write the code from the top of the Hello Mum - Windows Style - Part 4 page, we get the following code. Private Sub HelloPaint_Paint( ByVal sender As Object, _
ByVal e As PaintEventArgs) _
Handles MyBase.Paint
' Get Graphics Object
Dim g As Graphics = e.Graphics
' Create Font
Dim myFont As New Font( "Verdana", 20 )
' Create Brush
Dim myBrush As New SolidBrush( Color.Tomato )
' Draw the String
g.DrawString( "Hello Mum!", myFont, myBrush, 40, 40 )
' Now tidy up
myFont.Dispose()
myBrush.Dispose()
End Sub
You can download the file helloPaint.vb from the link, or you can download my copy of the executable from here. If you have Visual Studio, you can download the solution here. Here we create our Graphics objects, use them, then clean up after ourselves. ReferencesFor more information on the Graphics class, visit MSDN at microsoft here. For more information on the Font class, visit MSDN at microsoft here. For more information on the Brush class, visit MSDN at microsoft here. For more information on the Pen class, visit MSDN at microsoft here. For more information on the IDisposable.Dispose method, visit MSDN at microsoft here. What Next?Next we will draw some simple Lines. Return to the Tutorial Contents. |