| You are here: | About>Computing & Technology>Visual Basic> Using VB.NET> GDI+ Graphics in Visual Basic 2005 .NET |
![]() | Visual Basic |
GDI+ Graphics in Visual Basic 2005 .NETJan 27 2007 Part 1 of an About Visual Basic TutorialGDI+ 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 2005. But if you feel you need even more, you can check out this previous article. Some of the information there is repeated here to make sure that nothing is skipped. 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 the namespaces that are used in VB.NET for graphics programming. This is an area where Microsoft has totally confused the way things are named and described so it's hard to find useful information about using GDI+ in Visual Basic at Microsoft's MSDN site. But the fact is that you really can't do graphics without using GDI+. 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 2005 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.
The place to start with GDI+ is the Graphics object. Although the things you draw show up on your monitor or a printer, the Graphics object is the "canvas" that you draw on. But the Graphics object is also one of the first sources of confusion when using GDI+. But the Graphics object is always associated with a particular device context. So the first problem that virtually every new student of GDI+ confronts is, "How do I get a Graphics object?" There are basically two ways. First ... You can use the e event parameter that is passed to the OnPaint event with the PaintEventArgs object. Several events pass the PaintEventArgs and you can use the to refer to the Graphics object that is already being used by the device context. Second ... You can use the CreateGraphics method for a device context to create a Graphics object. The first method was used in the earlier article mentioned above. Here's the example: Protected Overrides Sub OnPaint( _ -------- Add this into the Form1 class for a standard Windows Application to code it yourself. In this example, a Graphics object is already created for the form Form1. All your code has to do is create a local instance of that object and use it to draw on the same form. Notice that your code Overrides the OnPaint method. That's why MyBase.OnPaint(e) is executed at the end. You need to make sure that if the base object (the one you're overriding) is doing something else, it gets a chance to do it. Often, your code works without this, but it's a good idea. You can also get a Graphics object using the PaintEventArgs object handed to your code in the OnPaint and OnPaintBackground methods of a Form. The PrintPageEventArgs passed in a PrintPage event will contain a Graphics object for printing. It's even possible to get a Graphics object for some images. This can let you paint right on the image the same way you would paint on a Form or component. Another variation of method one is to add an event handler for the Paint event for the form. Here's what that code looks like: Private Sub Form1_Paint( _ The next page demonstrates the CreateGraphics method ... |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


