GDI+ (Graphic Device Interface Plus) is one of the biggest, and least understood changes in Visual Basic .NET -- especially for programmers coming from a Visual Basic 6 background. But we have a great way to help you get a firm grip on the concepts here: About Visual Basic's Tic Tac Toe game. Tic Tac Toe offers a practical use of GDI+ without being overwhelming and has already been featured in a detailed explanation of the use of the VB.NET "Upgrade Wizard" where GDI+ was introduced.
If you would like to review the detailed explanation of the original Tic Tac Toe game as programmed in VB 6: Click Here!
If you would like to read the step-by-step process of upgrading the VB 6 Tic Tac Toe using the Microsoft Upgrade Wizard: Click Here!
The explanation of a completely rewritten VB.NET/GDI+ version of the program is the final article in this series: Click Here!
And to download the completed source code developed in this article, Click Here!
There are two things about GDI+ that make it difficult for VB 6 programmers:
- GDI+ wasn't used in Visual Basic 6. Even Microsoft's official documentation states that Microsoft Windows GDI+ is an API for C/C++ programmers. But the fact is that you really can't do graphics without using GDI+. And it's as easy to use in VB.NET as it is in any other .NET language. The idea that GDI+ is something 'reserved' for 'curly brace' languages is just another myth that isn't true.
- The way you work with GDI+ is VERY object oriented and even a bit different than the way you work with other objects. So it's easy for someone just starting to learn about OOP to get lost.
First, let's get "up to speed" with GDI+ and just use it to add some text to some standard VB.NET components. In the previous article where the VB 6 to VB.NET Upgrade Wizard was discussed, we added lines to the Tic Tac Toe grid. We'll do something similar here, but with text.
So ... starting at the beginning ... let's go over just what GDI+ is actually used for.
GDI+ give you the ability to program with colors, fonts, geometric shapes, and graphical images. So, if you want to draw a shape or use fonts, give them color, texture, transparency or anything like that, GDI+ is your tool. Getting more advanced, you can write code to make these graphics do things, like triggering events when graphical objects are clicked or interacting with each other use drag-and-drop logic. You'll also find that GDI+ is very useful with ASP.NET as well.
Like other major Microsoft technologies, the term "GDI+" is really a whole group of software technologies. The individual groups are often called "namespaces" and the ones that are in GDI+ are:
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.
This gives you objects for more advanced two-dimensional vector graphics. Some of them are gradient brushes, pen caps, and geometric transforms.
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.
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.
You can use collections of fonts with this namespace.
I mentioned earlier that, although GDI+ - like the rest of .NET - is totally object oriented, it's a bit different than the way you work with other objects. In particular, you don't just drag and drop a GDI+ "component" onto a form. There is no GDI+ component. The thing to understand is that since other components use GDI+, you just slip your code in there beside code supplied by the component to get what you want done. Until you actually do it, that might sound a bit mysterious. So let's do a real example.