1. Home
  2. Computing & Technology
  3. Visual Basic

GDI+ Graphics in Visual Basic 2005 .NET
Dan's Program Code

By Dan Mabbutt, About.com

Aug 18 2007

The entire code is shown below. A button has to be added to a form, but other than that, there is no design work required.

Imports System.Drawing.Drawing2D
Imports System.Math
Public Class Form1
  Dim p As Pen = _
    New Pen(Color.Black, 1)
  Dim b As SolidBrush = New SolidBrush(Color.Black)
  Dim FillFlag As Boolean = False
  Private Sub Button1_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
    p.Color = Color.Red
    b.Color = Color.Blue
    FillFlag = True
    Me.Refresh()
  End Sub
  Private Sub Form1_Paint( _
    ByVal sender As Object, _
    ByVal e As System.Windows.Forms.PaintEventArgs) _
    Handles Me.Paint
    Dim g As Graphics = e.Graphics
    ' Dream up a sequence of Booleans and Colors to control
    ' the display of circles
    Dim dispCtrl() As Boolean = _
      {True, False, True, True, False, False, True, True, _
      True, False, False, False, True, True, False, False, _
      False, True, True, False, False, True, True, False, _
      True, True, False, False, True, True, False, False}
    Dim colorCtrl() As Color = _
      {Color.DarkGoldenrod, Color.DarkBlue, _
      Color.Khaki, Color.Maroon, _
      Color.Bisque, Color.IndianRed, _
      Color.SaddleBrown, Color.CadetBlue, _
      Color.Purple, Color.LavenderBlush, _
      Color.MediumVioletRed, Color.LawnGreen, _
      Color.Maroon, Color.Firebrick, _
      Color.OrangeRed, Color.Indigo, _
      Color.Moccasin, Color.MediumAquamarine, _
      Color.SandyBrown, Color.DarkOrchid, _
      Color.DarkTurquoise, Color.DarkSeaGreen, _
      Color.DarkOrange, Color.CadetBlue, _
      Color.DeepSkyBlue, Color.Salmon, _
      Color.MediumVioletRed, Color.LawnGreen, _
      Color.Maroon, Color.DeepPink, _
      Color.OrangeRed, Color.Indigo}
    Dim xcoord, ycoord As Single
    Dim segAngle As Double = 360 / 32
    Dim totAngle As Double = 0
    Dim radAngle As Double
    Dim c1Rad As Single = 100
    Dim c2Diam As Single
    Dim c1Circum As Single
    Dim xOffset As Single = 100
    Dim yOffset As Single = 50
    c1Circum = PI * c1Rad * 2
    c2Diam = c1Circum / 32
    For i As Integer = 0 To 31
      totAngle += segAngle
      radAngle = PI * totAngle / 180
      xcoord = c1Rad + c1Rad * Sin(radAngle) + xOffset
      ycoord = c1Rad - c1Rad * Cos(radAngle) + yOffset
      If dispCtrl(i) Then
        p.Color = colorCtrl(i)
        If FillFlag Then
          b.Color = colorCtrl(i)
          g.FillEllipse( _
            b, xcoord, ycoord, c2Diam, c2Diam)
        Else
          g.DrawEllipse( _
            p, xcoord, ycoord, c2Diam, c2Diam)
        End If
        g.DrawLine(p, _
          Convert.ToSingle(xOffset + c1Rad + c2Diam / 2), _
          Convert.ToSingle(yOffset + c1Rad + c2Diam / 2), _
          Convert.ToSingle(xcoord + c2Diam / 2), _
          Convert.ToSingle(ycoord + c2Diam / 2))
      End If
    Next
  End Sub
End Class

Explore Visual Basic
By Category
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Using VB.NET
  5. GDI+ Graphics in Visual Basic 2005 .NET

©2009 About.com, a part of The New York Times Company.

All rights reserved.