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:
To draw in a picture box:
To draw in a text box:
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
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:
Alternative method:
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:
- Create a new VB2019 Windows Forms Application project
- Drag a Button control onto the form
- Double-click the button and enter the following code:
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:
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)

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

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.