Lesson 28: Drawing Ellipses & Circles in VB2019

Master circular shapes with GDI+ using DrawEllipse method

Key Takeaway

Visual Basic 2019 uses the DrawEllipse method for both ellipses and circles, with circles being a special case where width and height are equal.

Building on the rectangle drawing techniques from the previous lesson, we'll now explore how to draw ellipses and circles in VB2019. These curved shapes are essential for creating everything from UI elements to game graphics.

Ellipse Fundamentals

Ellipses are defined by their bounding rectangles

Rectangle Objects

Create reusable Rectangle objects for consistent shapes

Direct Coordinates

Draw without creating rectangle objects

Circle Creation

Simply set width and height equal

28.1 Drawing Ellipses in VB2019

In Visual Basic 2019, you can draw ellipses using two primary methods similar to rectangles. Both approaches leverage the Graphics and Pen objects.

Method 1: Rectangle Object

First create a Rectangle object, then draw the ellipse using the DrawEllipse method.

Dim myRectangle As New Rectangle(X, Y, width, height) myGraphics.DrawEllipse(myPen, myRectangle)

Where:

  • X, Y: Coordinates of the upper-left corner
  • width: Width of the bounding rectangle
  • height: Height of the bounding rectangle

Method 2: Direct Drawing

Draw an ellipse directly by specifying its bounding rectangle coordinates.

myGraphics.DrawEllipse(myPen, X, Y, width, height)

Benefits:

  • More concise for single-use shapes
  • Faster implementation
  • Simpler code for simple drawings

Ellipse Drawing: Rectangle Object Method

This example creates a turquoise ellipse with a 5-pixel border using a Rectangle object:

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 a turquoise pen with 5px width
    Dim myPen As New Pen(Drawing.Color.DarkTurquoise, 5)
    
    ' Create a rectangle object
    Dim myRectangle As New Rectangle(40, 30, 200, 100)
    
    ' Draw the ellipse
    myGraphics.DrawEllipse(myPen, myRectangle)
End Sub
Ellipse drawn with VB2019

Figure 28.1: Ellipse drawn using rectangle object method

Ellipse Drawing: Direct Method

This approach draws an ellipse without creating a separate Rectangle object:

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 a turquoise pen with 5px width
    Dim myPen As New Pen(Drawing.Color.DarkTurquoise, 5)
    
    ' Draw ellipse at (40,30) with width=200, height=100
    myGraphics.DrawEllipse(myPen, 40, 30, 200, 100)
End Sub

The direct method approach is particularly useful when:

  • You need to draw a single ellipse quickly
  • Your coordinates are already defined
  • You want to minimize code complexity

28.2 Drawing Circles

In VB2019, circles are simply a special case of ellipses where the width and height of the bounding rectangle are equal. The same DrawEllipse method is used for both shapes.

Circle Drawing: Rectangle Object Method

This example creates a circle by setting the rectangle's width and height equal:

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 a turquoise pen with 5px width
    Dim myPen As New Pen(Drawing.Color.DarkTurquoise, 5)
    
    ' Create a square rectangle for the circle
    Dim myRectangle As New Rectangle(90, 30, 100, 100)
    
    ' Draw the circle
    myGraphics.DrawEllipse(myPen, myRectangle)
End Sub
Circle drawn with VB2019

Figure 28.2: Circle drawn using rectangle object method

Circle Drawing: Direct Method

This approach draws a circle without creating a separate Rectangle object:

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 a turquoise pen with 5px width
    Dim myPen As New Pen(Drawing.Color.DarkTurquoise, 5)
    
    ' Draw circle at (90,30) with width=100, height=100
    myGraphics.DrawEllipse(myPen, 90, 30, 100, 100)
End Sub
Method Best For Performance Code Complexity
Rectangle Object Reusable shapes, complex designs Optimal for multiple shapes Slightly higher
Direct Drawing Single shapes, simple projects Faster for one-off shapes Lower

Lesson Summary

In this lesson, you've learned how to create ellipses and circles in VB2019:

Ellipse Fundamentals

Ellipses are defined by their bounding rectangles

Rectangle Objects

Create reusable Rectangle objects for consistent shapes

Direct Drawing

Draw ellipses directly without creating rectangle objects

Circle Creation

Set width and height equal to create perfect circles

With ellipse and circle drawing mastered, you're ready to explore more complex shapes. In the next lesson, we'll learn to draw text and create custom fonts.

Next Lesson

Learn to draw text and customize fonts in Lesson 29: Drawing Text.

Related Resources

VB2019 Graphics Guide

Comprehensive guide to all graphics capabilities in VB2019

Explore Guide

Graphics Sample Code

Practical VB2019 code samples for graphics programming

View Examples

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon

VB2019 Kindle Edition

Digital version of Visual Basic 2019 Made Easy

Get on Kindle