1. Computing

GDI+ Graphics in Visual Basic 2005 .NET

Part 8 of an About Visual Basic Tutorial

By

Updated March 10, 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
  • Part 7 - Colors and Transparency

In Part 8, we dig deep into the code for images. We have already used images in this tutorial. In Part 3 a picture of my pet lizard was used as the fill for a very thick "line" just to show how shapes could be stretched to meet different needs. And in Part 4, I used two transparent GIF images overlaid in the center of a "temperature" display. But these were just handy ways to describe how images could be integrated with vector graphics. This segment covers images as the focus, not as a side note.

The first thing to learn about an image in VB.NET is just what it actually is. In programming terms, it's probably best not to think of it as a file (like a JPG or BMP file). Think of an image as either ...

  • An object inherited from the Image class
  • A bit map in memory

If you look up the Image class in Microsoft's documentation, the first words describing the class are, "An abstract base class ..." So the first step in understanding what an Image is in VB.NET is to understand what an abstract class is. Microsoft says they are classes that are, "solely for the purpose of inheritance—to define features of derived, non-abstract classes."

Think of them as the mold that you pour software into.

An abstract class uses the keyword MustInherit in the definition. This can be confusing because what it actually means is that you can't simply instantiate the class as you normally would. So, for example, if you code:

Dim myImage As New Image

The compiler flags this as an error with the message:

'New' cannot be used on a class that is declared 'MustInherit'.

Instead, you need to instantiate a class that is inherited from the Image class. There are two to choose from:

  • BitMap
  • Metafile

(It's also interesting to note that, although the BitMap and Metafile classes are both inherited directly from Image, they are in different namespaces. Bitmap is in System.Drawing and Metafile is in System.Drawing.Imaging.)

So you can code:

Dim myBitmap As New Bitmap("D:\aBitmap.bmp")

Metafile is a bit more difficult than Bitmap. For one thing, the good folks at Microsoft haven't given us a way to encode Metafile objects in the .NET Framework yet (as of Framework version 2.0). So you can read Metafile files, but you can't write them directly.

Here's what Microsoft MSDN has to say about it:

When you use the Save method to save a graphic image as a Windows Metafile Format (WMF) or Enhanced Metafile Format (EMF) file, the resulting file is saved as a Portable Network Graphics (PNG) file instead. This behavior occurs because the GDI+ component of the .NET Framework does not have an encoder that you can use to save files as .wmf or .emf files.

You can, however, create metafiles and the next segment will have more to say about that!

The Icon class can be thought of as a type of bitmap too, but it's not an Image object and you have to use different methods to handle it. I'll discuss this class after we check out the Image class.

The Bitmap object in code creates the bitmap in memory ... a pixel by pixel representation of the image. There are a lot of useful methods provided in VB.NET to help you use this image. For example, you can create a thumbnail of the image:

Dim myImage As Bitmap = _
   New Bitmap("C:\zion.jpg", True)
PictureBoxThumb.Image = _
   myImage.GetThumbnailImage( _
   79, 38, Nothing, Nothing)
PictureBox.Image = myImage

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

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

©2014 About.com. All rights reserved.