Graphics for Visual Basic 6.0 Users
In Visual Basic 6.0, various graphics methods and properties are used to draw on a Form or PictureBox control. Graphics in Visual Basic 6.0 are based on the Windows Graphics Device Interface (GDI) APIs.
In Visual Basic 2008, graphics are provided by the System.Drawing namespace, which encapsulates GDI+ APIs. GDI+ expands on the graphics capabilities of Visual Basic 6.0, although the methods are not compatible.
Conceptual Differences
In Visual Basic 6.0, graphics methods apply only to the Form object and to the PictureBox control.
In Visual Basic 2008, graphics methods apply to forms, plus any control that supports the Paint event, including the PictureBox, Panel, and GroupBox controls. Additionally, graphics methods apply to any controls that support the OwnerDraw property, including the ListView, TreeView, and Button controls.
AutoRedraw Property
In Visual Basic 6.0, graphics methods can be called from any event procedure; the AutoRedraw property is used to persist graphics when graphics methods are called from an event other than the Paint event.
In Visual Basic 2008, graphics methods should only be called from the Paint event procedure, or in the case of some owner-drawn controls, from the various Draw event procedures (DrawItem, DrawSubItem, etc.). The AutoRedraw property is no longer supported and is not necessary because the Paint and Draw events automatically persist graphics.
ClipControls Property
In Visual Basic 6.0, the ClipControls property is used to control the painting of a form or control. When set to True, only newly exposed areas are repainted, in theory, improving performance.
There is no equivalent for the ClipControls property in Visual Basic 2008; performance enhancements in GDI+ and up-to-date video adapters make it unnecessary.
DrawMode Property
In Visual Basic 6.0, the DrawMode property controls the color of a graphics object when drawing one pattern on top of another. This property only affects monochrome or low-resolution displays (256 colors or less).
There is no equivalent for the DrawMode property in Visual Basic 2008; it is no longer necessary with current displays.
DrawStyle Property
In Visual Basic 6.0, the DrawStyle property controls the appearance of a line drawn using the Line method. If the DrawWidth property is set to a value greater than 1, the DrawStyle property has no effect and the line will always be solid.
In Visual Basic 2008, the appearance of a line is controlled by setting the DashStyle property of a System.Drawing.Pen class used by one of the DrawLine methods; line width has no bearing on this property.
DrawWidth Property
In Visual Basic 6.0, the DrawWidth property determines the thickness of a line in pixels; the DrawWidth property is typically set before performing a graphics method.
In Visual Basic 2008, the Pen.Width property of a System.Drawing.Pen control determines line thickness; you can set the Width property as a parameter when you create the Pen, or by setting Pen.Width after the Pen is created. If no Pen.Width property is specified, the default is 1 pixel.
Image Property
In Visual Basic 6.0 the Image property of a form or PictureBox control returns a handle to a bitmap; the handle can be assigned to the Picture property or used as a value to pass to Windows API calls.
In Visual Basic 2008, bitmaps no longer have handles; the actual bitmap itself is passed as an object of type Bitmap. A Bitmap control can be assigned to the Image property of a PictureBox control, but it cannot be passed to Windows API calls.
Line Method
In Visual Basic 6.0, the Line method is used to draw a rectangle by specifying the top left and lower coordinates, along with an optional argument B. The FillColor property is used to fill a rectangle with a solid color, and the FillStyle property fills the rectangle with a crosshatch pattern.
In Visual Basic 2008, the DrawRectangles method is used to draw the border of a rectangle, and the FillRectangle method is used to fill it. FillRectangle takes a Brush object as a parameter. The SolidBrush replaces the FillColor property and members of the HatchBrush class replace the FillStyle property.
Point Method
In Visual Basic 6.0, the Point method of a form or PictureBox control is used to return a color value for the pixel at a specified point. Although the Point method can be used for forms or controls that do not contain a picture, it is most often used to retrieve a color from a bitmap assigned to the Picture property.
In Visual Basic 2008, the Point method no longer exists. You can use the M:System.Drawing.Bitmap.GetPixel(System.Int32,System.Int32) method to retrieve a color value from a bitmap. For forms or controls that do not contain a picture, you can query the BackColor property.
Print Method
In Visual Basic 6.0, the Print method is used to display text on a form or PictureBox control. The font used to display the text is determined by the Font properties of the form or control, and the color is determined by the ForeColor property. The Print method offers no control for the location of the text and can only display text horizontally.
In Visual Basic 2008, the DrawString method is used to display text. The font is determined by a Font object, and the color is determined by a Brush object; both are passed as parameters to the DrawString method. The DrawString method also has X and Y parameters that determine the starting location for the text. There is also an optional Format parameter that takes a StringFormat object, allowing you to display text vertically.
PSet Method
In Visual Basic 6.0, the PSet method is used to change the color of a pixel on a form or PictureBox control. If the DrawWidth property is set to a value greater than 1, the PSet method draws a filled circle. An optional parameter is used to specify the color; if omitted ForeColor is used.
In Visual Basic 2008, there is no equivalent for the PSet method. To change the color of a single pixel on a form or the PictureBox control, use the DrawEllipse method to draw a circle with a height and width of 1 pixel. To duplicate the functionality of PSet when DrawWidth is greater than 1, use the FillEllipse method.
Code Changes for Graphics
The following code examples illustrate the differences in coding techniques between Visual Basic 6.0 and Visual Basic 2008.
Drawing a Simple Line
The following code demonstrates drawing a line on a form at run time. In the Visual Basic 6.0 example, the Line method is used; it takes the X and Y coordinates of the starting and ending points and, optionally, a color as arguments. The Visual Basic 2008 example uses the DrawLine method, which takes a Pens object and the X and Y coordinates of the starting and ending points as arguments.
Note
In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008, it is pixels.
' Visual Basic 6.0
Private Sub Form_Paint()
' Draw a solid black line 200 twips from the top of the form.
Line (0, 200) - (ScaleWidth, 200), vbBlack
End Sub
' Visual BasicPrivateSub Form1_Paint(ByVal sender AsObject, ByVal e _
As System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
' Draw a solid black line 25 pixels from the top of the form.
e.Graphics.DrawLine(Pens.Black, 0, 25, Me.Width, 25)
EndSub
Drawing a Dotted Line
The following code demonstrates drawing a dotted line on a form at run time. In the Visual Basic 6.0 example, the DrawStyle property determines the appearance of the line. The Visual Basic 2008 example uses a Pen object, setting the DashStyle property to determine the appearance.
Note
In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008, it is pixels.
' Visual Basic 6.0
Private Sub Form_Paint()
' Draw a dotted line 200 twips from the top of the form.
Me.DrawStyle = vbDot
Line (0, 200) - (ScaleWidth, 200), vbBlack
End Sub
' Visual BasicPrivateSub Form1_Paint1(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
' Draw a dotted black line 25 pixels from the top of the form.Dim LPen AsNew System.Drawing.Pen(System.Drawing.Color.Black)
LPen.DashStyle = Drawing2D.DashStyle.Dot
e.Graphics.DrawLine(LPen, 0, 25, Me.Width, 25)
EndSub
Controlling Line Thickness
The following code demonstrates drawing lines of different thicknesses on a form at run time. In the Visual Basic 6.0 example, the DrawWidth property is used. The Visual Basic 2008 example uses the Width property of the Pens object.
' Visual Basic 6.0
Private Sub Form_Paint()
' Draw a line with a thickness of 1 pixel.
DrawWidth = 1
Line (0, 200)-(ScaleWidth, 200), vbBlack
' Draw a line with a thickness of 5 pixels.
DrawWidth = 5
Line (0, 400)-(ScaleWidth, 400), vbBlack
' Draw a line with a thickness of 10 pixels.
DrawWidth = 10
Line (0, 600)-(ScaleWidth, 600), vbBlack
End Sub
' Visual BasicPrivateSub Form1_Paint2(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
' Draw a line with a thickness of 1 pixel.Dim TPen AsNew System.Drawing.Pen(System.Drawing.Color.Black, 1)
e.Graphics.DrawLine(TPen, 0, 25, Me.Width, 25)
' Draw a line with a thickness of 5 pixels.
TPen.Width = 5
e.Graphics.DrawLine(TPen, 0, 50, Me.Width, 50)
' Draw a line with a thickness of 10 pixels.
TPen.Width = 10
e.Graphics.DrawLine(TPen, 0, 75, Me.Width, 75)
EndSub
Drawing a Circle
The following code demonstrates drawing a circle on a form at run time. In the Visual Basic 6.0 example, the Circle method is used; it takes the X and Y coordinates of the center point, the radius, and, optionally, a color, as arguments. The Visual Basic 2008 example uses the DrawEllipse method, which takes a Pen object, the X and Y coordinates of the upper-left corner of the bounding rectangle, and the width and height as arguments.
Note
In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008, it is pixels.
' Visual Basic 6.0
Private Sub Form_Paint()
' Draw a 1000 twip diameter red circle
Circle (500, 500), 500, vbRed
End Sub
' Visual BasicPrivateSub Form1_Paint3(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
' Draw a 70 pixel diameter red circle.
e.Graphics.DrawEllipse(Pens.Red, 0, 0, 70, 70)
EndSub
Drawing a Filled Rectangle
The following code demonstrates drawing two rectangles on a form at run time, one with a solid fill and the other with a cross-hatched pattern. In the Visual Basic 6.0 example, the FillColor and FillStyle properties are used along with the Line method. Calling the Line method with the B parameter draws a rectangle.
The Visual Basic 2008 example uses the Graphics.Rectangle method to draw the outline, and the Graphics.FillRectangle method which takes a Brush object as an argument. In this example both SolidBrush and HatchBrush controls are used.
Note
In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008 it is pixels.
' Visual Basic 6.0
Private Sub Form_Paint()
' Draw a solid red rectangle.
FillColor = vbRed
FillStyle = vbSolid
Line (10, 10)- (1000, 500), vbRed, B
' Draw a rectangle filled with a crosshatch pattern.
FillColor = vbBlack
FillStyle = vbCross
Line (10, 500)- (1000, 1000), vbBlack, B
End Sub
' Visual BasicPrivateSub Form1_Paint4(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
' Draw a solid red rectangle.Dim SBrush AsNew System.Drawing.SolidBrush _
(System.Drawing.Color.Red)
e.Graphics.DrawRectangle(Pens.Red, 2, 2, 70, 40)
e.Graphics.FillRectangle(SBrush, 2, 2, 70, 40)
' Draw a rectangle filled with a crosshatch pattern.Dim HBrush AsNew System.Drawing.Drawing2D.HatchBrush( _
System.Drawing.Drawing2D.HatchStyle.Cross, _
System.Drawing.Color.Black, System.Drawing.Color.Transparent)
e.Graphics.DrawRectangle(Pens.Black, 2, 40, 70, 40)
e.Graphics.FillRectangle(HBrush, 2, 40, 70, 40)
EndSub
Displaying an Image on a Form
The following code demonstrates graphics methods for displaying an image on a form at run time. The Visual Basic 6.0 example uses the PaintPicture method. The Visual Basic 2008 example uses the DrawImage method.
' Visual Basic 6.0
Private Sub Form_Paint()
' Create a stdPicture object.
Dim Pict1 As New stdPicture
Pict1 = LoadPicture("C:\Windows\Greenstone.bmp")
PaintPicture Pict1, 0, 0
End Sub
' Visual BasicPrivateSub Form1_Paint5(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
' Create a Bitmap object.Dim Pict1 AsNew Bitmap("C:\Windows\Greenstone.bmp")
e.Graphics.DrawImage(Pict1, 0, 0)
EndSub
Displaying Text on a Form
The following code demonstrates graphics methods for displaying a text string on a form at run time. The Visual Basic 6.0 example uses the Print method. The Visual Basic 2008 example uses the DrawString method.
' Visual Basic 6.0
Private Sub Form_Paint()
Me.Font.Size = 24
Me.Font.Bold = True
Me.ForeColor = vbRed
Print "Hello World!"
End Sub
' Visual BasicPrivateSub Form1_Paint6(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
Dim TextFont AsNew System.Drawing.Font("Arial", 24, FontStyle.Bold)
Dim TextBrush AsNew System.Drawing.SolidBrush(System.Drawing.Color.Red)
e.Graphics.DrawString("Hello World!", TextFont, TextBrush, 10, 10)
TextFont.Dispose()
TextBrush.Dispose()
EndSub
Determining the Height and Width of a String
The following code demonstrates graphics methods for determining the size of a string on a form at run time, and then drawing a rectangle around it. The Visual Basic 6.0 example uses the TextHeight and TextWidth methods. The Visual Basic 2008 example uses the MeasureString method, which returns a SizeF structure.
' Visual Basic 6.0
Private Sub Form_Paint()
Me.Font.Size = 24
Me.Font.Bold = True
Me.ForeColor = vbRed
Print "Hello World!"
Line (0, 0)-(TextWidth("Hello World!"), _
TextHeight("Hello World!")), vbBlack, B
End Sub
' Visual BasicPrivateSub Form1_Paint7(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
Dim TextFont AsNew System.Drawing.Font("Arial", 24, FontStyle.Bold)
Dim TextBrush AsNew System.Drawing.SolidBrush(System.Drawing.Color.Red)
e.Graphics.DrawString("Hello World!", TextFont, TextBrush, 10, 10)
Dim TextSize AsNew System.Drawing.SizeF
TextSize = e.Graphics.MeasureString("Hello World!", TextFont)
e.Graphics.DrawRectangle(Pens.Black, 10, 10, TextSize.Width, TextSize.Height)
TextFont.Dispose()
TextBrush.Dispose()
EndSub
Drawing a Single Pixel
The following example demonstrates graphics methods for changing the color of a single pixel on a form at run time. The Visual Basic 6.0 example uses the PSet method. The Visual Basic 2008 example uses the DrawEllipse method with the Height and Width parameters set to 1.
Note In Visual Basic 6.0, the default unit of measurement is twips; in Visual Basic 2008 it is pixels.
' Visual Basic 6.0
Private Sub Form_Paint()
Me.DrawWidth = 1
PSet (1000, 1000), vbRed
End Sub
' Visual BasicPrivateSub Form1_Paint8(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
e.Graphics.DrawEllipse(Pens.Red, 70, 70, 1, 1)
EndSub
Determining the Color of a Single Pixel
The following code demonstrates graphics methods for determining the color of a pixel at a specified location in an image on a form at run time, and then drawing a rectangle filled with that color. The Visual Basic 6.0 example uses the Point method to retrieve the color value. The Visual Basic 2008 example uses the GetPixel method.
Note
In Visual Basic 6.0 the default unit of measurement is twips; in Visual Basic 2008, it is pixels.
' Visual Basic 6.0
Private Sub Form_Paint()
Dim PixelColor As Long
Picture1.Picture = LoadPicture("C:\Windows\Greenstone.bmp")
PixelColor = Picture1.Point(10, 10)
FillColor = PixelColor
Line (0, 0)-(100, 500), PixelColor, B
End Sub
' Visual BasicPrivateSub Form1_Paint9(ByVal sender AsObject, ByVal e As _
System.Windows.Forms.PaintEventArgs) HandlesMyBase.Paint
Dim Pict1 AsNew Bitmap("C:\Windows\Greenstone.bmp")
Picture1.Image = Pict1
Dim PixelColor As Color = Pict1.GetPixel(4, 4)
Dim PixelBrush AsNew SolidBrush(PixelColor)
e.Graphics.FillRectangle(PixelBrush, 0, 0, 100, 100)
EndSub
Graphics Property and Method Equivalencies
The following table lists Visual Basic 6.0 graphics properties and methods, along with their Visual Basic 2008 equivalents.
Visual Basic 6.0 |
Visual Basic 2008 Equivalent |
---|---|
AutoRedraw property |
New implementation. To persist graphics, put graphics methods in the Paint event. |
Circle method |
DrawEllipse method |
ClipControls property |
New implementation. The ClipControls property is no longer necessary. |
Cls method |
Clear method |
CurrentX property |
The x parameter of various graphics methods. For example, DrawRectangle(pen, x, y, width, height) |
CurrentY property |
The y parameter of various graphics methods. For example, DrawRectangle (pen, x, y, width, height) |
DrawModeproperty |
New implementation. The DrawMode property is no longer necessary. |
DrawStyle property |
DashStyle property |
DrawWidth property |
Width property |
FillColor property |
SolidBrush object |
FillStyle property |
HatchBrush object |
HasDC property |
New implementation. Device contexts are no longer necessary with GDI+. |
HDC property |
New implementation. Device contexts are no longer necessary with GDI+. |
Image property |
New implementation. |
Line method |
DrawLine method |
PaintPicture method |
DrawImage method |
Point method |
No direct equivalent. For bitmaps, use Bitmap.GetPixel. For forms or controls, use the BackColorproperty. |
Print method |
DrawString method |
Pset method |
DrawEllipse, FillEllipse methods |
TextHeight, TextWidth properties |
MeasureString method |
Upgrade Notes
When an application is upgraded from Visual Basic 6.0 to Visual Basic 2008, graphics methods are not upgraded, and warnings are inserted into the code. Due to the considerable differences between GDI and GDI+, any existing graphics code will need to be rewritten.
See Also
Tasks
Custom Drawing User Control Sample
Other Resources
Graphics Overview (Windows Forms)