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

GDI+ Graphics in Visual Basic 2005 .NET
Part 7 of an About Visual Basic Tutorial

By , About.com Guide

Mar 4 2007

GDI+ is the way to draw shapes, fonts, images or generally anything graphic in Visual Basic .NET. You can get the most from this tutorial by starting with Part 1 where the fundamentals of GDI+ are explained.

Previous Lesson Links

  • Part 1 - Introduction to GDI+ and the Graphics object
  • Part 2 - Drawing a curve with a series of connected points
  • Part 3 - Advanced vector graphics with an intro to bitmapped graphics
  • Part 4 - Blending and merging colors and shapes in the same graphic
  • Part 5 - Coordinate Spaces and Matrix Transformations
  • Part 6 - Using the GraphicsPath object and clipping regions

Color is obviously a big part of computer graphics and we've used it all along in this series, but I haven't focused on it in previous parts of the tutorial. We're going to make up for that now. A natural part of that discussion is the concept of transparency, too. So I'll show how that works as well.

So far, you've seen color in statements like this (from Part 3):

g.DrawLine(New Pen(Color.Crimson, 3), 10, 10, 100, 40)

A lot of GDI+ objects, like Pen, require a color as part of the New instantiation of the object. The colors we have used so far are called Argb colors (Alpha, Red, Green, Blue) or sometimes GDI+ colors. The named colors, like Crimson above, are called System defined colors, but they're far from being the only ones. These are just provided for your convenience so you can use a name instead of a structure of numbers to refer to colors. The Argb colors are actually all of the members of the Color structure. If you need to declare a Color in your program, you do it the same way you would declare any other structure type:

Dim myColor as Color

The Color structure is actually a 32-bit number that is made up of four 8-bit components (so the values can range from 0 (binary 00000000) to to 255 (binary 11111111):

  • alpha
  • red
  • green
  • blue

Alpha is the transparency of the color. All of the system defined colors have a transparency equal to 255 or opaque. A completely transparent color has an alpha value of 0.

You can define any Argb color using the FromArgb method. This gives you a kind of funky light gray line (the arguments were chosen at random):

Dim myColor As Color = _
   Color.FromArgb(64, 128, 192, 255)
g.DrawLine( _
   New Pen(myColor, 3), 10, 10, 100, 40)

The Argb value is sometimes known as the blending value because you can use it to blend colors together. Using solid red as the Red-Blue-Green part of the Argb color (255, 0, 0) and one third of 255, or 85 as the alpha value, we can blend three red circles together to see how the effect works in action. The center is an approximation of solid red.

Dim OneThirdArgb As Color = Color.FromArgb(85, 255, 0, 0)
Dim OneThirdArgbBrush As New SolidBrush(OneThirdArgb)
Dim mySolidBrushRed As New SolidBrush(Color.Red)
g.FillEllipse(OneThirdArgbBrush, 10, 10, 100, 100)
g.FillEllipse(OneThirdArgbBrush, 60, 10, 100, 100)
g.FillEllipse(OneThirdArgbBrush, 40, 60, 100, 100)
g.FillEllipse(mySolidBrushRed, 175, 10, 100, 100)

The result is shown below. A system defined sold red circle is drawn beside it for comparison.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Unfortunately, the Argb color system used by GDI+ is not the only way colors are used in Visual Basic programs. Another type of color coding is commonly called HTML colors. This (very) brief web page will display a page with a background color of hot pink.

<html>
<head>
<title>Colors</title>
</head>
<body bgcolor="#FF3399">
Put on your sun glasses!
</body>
</html>

About Visual Basic has a Visual Basic 6 based article, Calculating a Contrasting Color Code, that uses this type of color coding extensively. Since HTML colors are not VB.NET Color objects, they have to be converted before they can be used in a VB.NET program as colors:

Dim myColor As Color = ColorTranslator.FromHtml("#FF3399")

If you want to use the Argb color code in a calculation (as the VB 6 based article above does), then you can convert it into an integer. There is a companion ToHtml method for converting back again.

Dim myRedColor As Integer = Color.Red.ToArgb()

The result is an Integer equal to -65536! Since we know that the red Color object is a 32 bit number and we know the hex equivalent is FFFF0000, a consideration of the internal coding of an Integer means that high order bit 1 makes it a negative number and a negative FFFF0000 is 65536. (If you don't understand positive and negative integers, figuring out why this is true will make it all clear to you.)

It would be nice if this was the only other color system that you might have to deal with, but unfortunately, we're not finished yet. There are also two color schemes called Ole color and Win32 color! Even though VB.NET has separate methods to handle them, they're actually the same. (If you code a loop to try out all 4,294,967,296 color combinations, you will discover that the ToWin32 and ToOle methods create exactly the same color codes.) They're typically used by the previous generation of software such as forms and controls in VB 6.

These formats code color as an integer value where red, blue, and green color values are combined to created a unique color value for each color. You often see this formula:

OLEColor = Blue Value * 65536 + Green Value * 256 + Red Value

Don't be fooled. That just means that it's the same color coding as GDI+ color ... but it's not a VB.NET structure and the colors are in a different sequence. As an example, this code will draw a blue circle:

Dim myColor As Color = ColorTranslator.FromOle(CInt(&HFF0000))
g.FillEllipse(New SolidBrush(myColor), 10, 10, 100, 100)

Notice that the FF is simply the code for blue and the other two colors are not present.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. 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.