Lesson 31: Coloring Shapes in VB2019

Master shape coloring with GDI+ using FillRectangle, FillEllipse and FillPolygon methods

Key Takeaway

Visual Basic 2019 uses SolidBrush objects to fill shapes with vibrant colors, creating visually appealing graphics beyond simple outlines.

After mastering polygon and pie shapes in the previous lesson, we'll now learn how to fill shapes with colors in VB2019. Adding color brings your graphics to life and enhances visual communication.

SolidBrush Fundamentals

Create color-filled shapes with SolidBrush objects

FillRectangle Method

Fill rectangular shapes with solid colors

FillEllipse Method

Fill circular and elliptical shapes

FillPolygon Method

Fill complex polygonal shapes

31.1 Creating SolidBrush Objects

To fill shapes with colors in VB2019, you need to create a SolidBrush object that defines the fill color. The syntax is:

Syntax.vb
Dim myBrush As New SolidBrush(Color.ColorName)
Parameter Description Required
ColorName Name of the color (e.g., Coral, Blue, Green) Yes

You can access a wide range of predefined colors through IntelliSense after typing "Color."

31.2 Filling Rectangles with Color

The FillRectangle method fills a rectangular area defined by coordinates and dimensions with the specified brush color.

Example 31.1: Filling a Rectangle

This example creates a coral-filled rectangle with a blue outline:

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 blue pen with 5px thickness
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Create a coral brush
    Dim myBrush As New SolidBrush(Color.Coral)
    
    ' Draw and fill the rectangle
    myGraphics.DrawRectangle(myPen, 65, 50, 150, 150)
    myGraphics.FillRectangle(myBrush, 65, 50, 150, 150)
End Sub

With Outline

Outline + Fill

Without Outline

Solid fill only

Colored rectangle on form

Figure 31.1: Rectangle filled with color

31.3 Filling Ellipses with Color

The FillEllipse method fills an elliptical area defined by a bounding rectangle with the specified brush color.

Example 31.2: Filling an Ellipse

This example creates a coral-filled ellipse with a blue outline:

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 blue pen with 5px thickness
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Create a coral brush
    Dim myBrush As New SolidBrush(Color.Coral)
    
    ' Draw and fill the ellipse
    myGraphics.DrawEllipse(myPen, 50, 50, 180, 100)
    myGraphics.FillEllipse(myBrush, 50, 50, 180, 100)
End Sub

With Outline

Outline + Fill

Without Outline

Solid fill only

Colored ellipse on form

Figure 31.2: Ellipse filled with color

31.4 Filling Polygons with Color

The FillPolygon method fills a polygonal area defined by an array of points with the specified brush color.

Example 31.3: Filling a Polygon

This example creates a coral-filled quadrilateral with a blue outline:

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
    
    ' Define polygon vertices
    Dim A As New Point(70, 10)
    Dim B As New Point(170, 50)
    Dim C As New Point(200, 150)
    Dim D As New Point(140, 200)
    Dim myPoints As Point() = {A, B, C, D}
    
    ' Create a blue pen with 5px thickness
    Dim myPen As New Pen(Drawing.Color.Blue, 5)
    
    ' Create a coral brush
    Dim myBrush As New SolidBrush(Color.Coral)
    
    ' Draw and fill the polygon
    myGraphics.DrawPolygon(myPen, myPoints)
    myGraphics.FillPolygon(myBrush, myPoints)
End Sub

With Outline

Outline + Fill

Without Outline

Solid fill only

Colored polygon on form

Figure 31.3: Polygon filled with color

Lesson Summary

In this lesson, you've learned how to fill shapes with colors in VB2019:

SolidBrush Objects

Create color-filled shapes with SolidBrush

FillRectangle

Fill rectangular shapes with solid colors

FillEllipse

Fill circular and elliptical shapes

FillPolygon

Fill complex polygonal shapes

With shape coloring mastered, you're ready to explore dynamic graphics. In the next lesson, we'll learn to use timers for animations.

Next Lesson

Learn to create dynamic applications in Lesson 32: Using Timer.

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