1. Technology

GDI+ Graphics in Visual Basic .NET

Part 1 of an About Visual Basic Tutorial

By

Updated June 12, 2014

GDI+ is the way to draw shapes, fonts, images or generally anything graphic in Visual Basic .NET.

This article is the first part of a complete introduction to using GDI+ in Visual Basic .NET.

GDI+ is an unusual part of .NET. It was here before .NET (GDI+ was released with Windows XP) and it doesn't share the same update cycles as the .NET Framework. Microsoft's documentation usually states that Microsoft Windows GDI+ is an API for C/C++ programmers into the Windows OS. But GDI+ is also includes the namespaces used in VB.NET for software based graphics programming.

But it's not the only graphics software provided by Microsoft, especially since Framework 3.0. When Vista and 3.0 were introduced, the totally new WPF was introduced with it. WPF is a high-level, hardware accelerated approach to graphics. As Tim Cahill, Microsoft WPF software team member puts it, with WPF "you describe your scene using high-level constructs, and we’ll worry about the rest." And the fact that it's hardware accelerated means that you don't have to drag down the operation of your PC processor drawing shapes on the screen. Much of the real work is done by your graphics card.

We've been here before, however. Every "great leap forward" is usually accompanied by a few stumbles backward, and besides, it will take years for WPF to work it's way through the zillions of bytes of GDI+ code. That's especially true since WPF just about assumes that you're working with a high-powered system with lots of memory and a hot graphics card. That's why many PC's couldn't run Vista (or at least, use the Vista "Aero" graphics) when it was first introduced. So this series continues to be available on the site for any and all who continue to need to use it.

GDI+ isn't something that you can drag onto a form like other components in VB.NET. Instead, GDI+ objects generally have to be added the old way ... by coding them from scratch! (Although, VB .NET does include a number of very handy code snippets that can really help you. Check this article for more on code snippets.)

To code GDI+, you use objects and their members from a number of .NET namespaces. (At the present time, these are actually just wrapper code for Windows OS objects which actually do the work.)

The namespaces in GDI+ are:

System.Drawing

This is the core GDI+ namespace. It defines objects for basic rendering (fonts, pens, basic brushes, etc.) and the most important object: Graphics. We'll see more of this in just a few paragraphs.

System.Drawing.Drawing2D

This gives you objects for more advanced two-dimensional vector graphics. Some of them are gradient brushes, pen caps, and geometric transforms.

System.Drawing.Imaging

If you want to change graphical images - that is, change the palette, extract image metadata, manipulate metafiles, and so forth - this is the one you need.

System.Drawing.Printing

To render images to the printed page, interact with the printer itself, and format the overall appearance of a print job, use the objects here.

System.Drawing.Text

You can use collections of fonts with this namespace.

  1. About.com
  2. Technology
  3. Visual Basic
  4. Using VB.NET
  5. GDI+ Graphics in Visual Basic .NET Tutorial

©2014 About.com. All rights reserved.