Lesson 26: Creating Graphics in VB2019

Learn to create graphics, draw shapes, and work with the GDI+ framework

Key Takeaway

Visual Basic 2019 provides powerful graphics capabilities through the GDI+ framework, allowing you to create custom shapes, lines, and graphics programmatically.

Creating graphics in VB2019 is different from earlier versions of Visual Basic. While earlier versions had built-in drawing tools in the toolbox, VB2019 requires you to write code to create shapes and graphics. This approach offers more flexibility and power to create customized drawings.

GDI+ Framework

The foundation for all graphics operations in VB2019

Graphics Object

The canvas where all drawing operations occur

Pen Object

Defines the style, color and width of lines

Brush Object

Defines how shapes are filled with color

26.1 Introduction to Graphics in VB2019

Creating graphics was relatively easy in earlier versions of Visual Basic because they had built-in drawing tools. For example, in Visual Basic 6, the drawing tools were included in the toolbox where the programmer just needed to drag the shape controls into the form to create rectangles, squares, ellipses, circles, and more.

Since Visual Basic evolved into an object-oriented programming language under the .NET framework, shape controls are no longer available. Now the programmer needs to write code to create various shapes and drawings. Although the learning curve is steeper, the programmer can write powerful code to create all kinds of graphics. Visual Basic 2019 offers various graphics capabilities that enable programmers to write code that can create all kinds of shapes and even fonts.

Graphics Evolution

While VB6 had built-in shape controls, VB2019 uses the powerful GDI+ framework that provides more control and flexibility for creating complex graphics.

26.2 Creating the Graphics Object

Before you can draw anything on a form, you need to create the Graphics object in Visual Basic 2019. A graphics object is created using the CreateGraphics() method. You can create a graphics object that draws to the form itself or a control.

Graphics Object Creation

To draw graphics on the default form:

Dim myGraphics As Graphics = Me.CreateGraphics

To draw in a picture box:

Dim myGraphics As Graphics = PictureBox1.CreateGraphics

To draw in a text box:

Dim myGraphics As Graphics = TextBox1.CreateGraphics

The Graphics object that is created does not draw anything on the screen until you call the methods of the Graphics object. In addition, you need to create the Pen object as the drawing tool.

26.3 Creating a Pen

A Pen defines the style, color, and width of lines. You can create a Pen using the following code:

Pen Creation

myPen = New Pen(Brushes.Color, LineWidth)

Where myPen is a Pen variable. You can use any variable name. The first argument defines the color of the drawing line and the second argument defines the width of the drawing line.

Example: Create a pen that draws a dark magenta line with 10px width:

myPen = New Pen(Brushes.DarkMagenta, 10)

Alternative method:

Dim myPen As Pen
myPen = New Pen(Drawing.Color.Blue, 5)

Where the first argument defines the color (blue in this case) and the second argument is the width of the drawing line.

26.4 Drawing a Line

Now that we have our Graphics and Pen objects, we can draw a line on the form.

Drawing Lines

To draw a line:

  1. Create a new VB2019 Windows Forms Application project
  2. Drag a Button control onto the form
  3. Double-click the button and enter the following code:
Form1.vb
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click
    ' Create the Graphics object
    Dim myGraphics As Graphics = Me.CreateGraphics
    
    ' Create the Pen object
    Dim myPen As Pen
    myPen = New Pen(Brushes.DarkMagenta, 20)
    
    ' Draw a line from (60,180) to (220,50)
    myGraphics.DrawLine(myPen, 60, 180, 220, 50)
End Sub

The syntax of the DrawLine method is:

object.DrawLine(Pen, x1, y1, x2, y2)

Where:

  • Pen: The Pen object to draw with
  • x1, y1: The starting point coordinates (60, 180 in our example)
  • x2, y2: The ending point coordinates (220, 50 in our example)
Line drawn with VB2019

Figure 26.1: Line drawn using DrawLine method

Lesson Summary

In this lesson, you've learned the fundamentals of creating graphics in VB2019:

Graphics Object

Create a Graphics object using CreateGraphics() method

Pen Creation

Define line style, color and width with Pen objects

Drawing Lines

Use DrawLine method to create straight lines

GDI+ Framework

Understand the foundation of graphics programming in VB2019

Graphics programming opens up many possibilities for creating custom interfaces and visualizations. In the next lesson, we'll explore how to draw rectangles and other shapes.

Next Lesson

Learn to draw rectangles and other shapes in Lesson 27: Drawing Rectangles.

Related Resources

VB6 Graphics Tutorial

Learn graphics programming in Visual Basic 6

Explore Tutorials

Visual Basic Examples

Practical VB code samples for graphics programming

View Examples

Excel VBA Graphics

Create charts and graphics in Excel with VBA

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon