Sunday, October 20, 2019
GDI Graphics in Visual Basic .NET Tutorial
GDI Graphics in Visual Basic .NET Tutorial 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 doesnt share the same update cycles as the .NET Framework. Microsofts documentation usually states that Microsoft Windows GDI is an API for C/C programmers into the Windows OS. But GDI also includes the namespaces used in VB.NET for software-based graphics programming. WPF But its 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 its hardware accelerated means that you dont 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. Weve 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 its way through the zillions of bytes of GDI code. Thats especially true since WPF just about assumes that youre working with a high-powered system with lots of memory and a hot graphics card. Thats why many PCs couldnt 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. Good Ol Code GDI isnt 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.) 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.) Namespaces 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. Well 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. Graphics Object 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. 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: 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.You can use theà CreateGraphicsà method for a device context to create a Graphics object. Heres an example of the first method: Protected Overrides Sub OnPaint( _ à à à ByVal e As System.Windows.Forms.PaintEventArgs) à à à Dim g As Graphics e.Graphics à à à g.DrawString(About Visual Basic vbCrLf _ à à à and GDI vbCrLf A Great Team, _ à à à New Font(Times New Roman, 20), _ à à à Brushes.Firebrick, 0, 0) à à à MyBase.OnPaint(e) End Sub Click Here to display the illustration 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. Thats whyà MyBase.OnPaint(e)à is executed at the end. You need to make sure that if the base object (the one youre overriding) is doing something else, it gets a chance to do it. Often, your code works without this, but its a good idea. PaintEventArgs 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. Its 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. Event Handler Another variation of method one is to add an event handler for theà Paintà event for the form. Heres what that code looks like: Private Sub Form1_Paint( _ à à à ByVal sender As Object, _ à à à ByVal e As System.Windows.Forms.PaintEventArgs) _ à à à Handles Me.Paint à à à Dim g As Graphics e.Graphics à à à g.DrawString(About Visual Basic vbCrLf _ à à à and GDI vbCrLf A Great Team, _ à à à New Font(Times New Roman, 20), _ à à à Brushes.Firebrick, 0, 0) End Sub CreateGraphics The second method to get a Graphics object for your code uses aà CreateGraphicsà method that is available with many components. The code looks like this: Private Sub Button1_Click( _ à à à ByVal sender As System.Object, _ à à à ByVal e As System.EventArgs) _ à à à Handles Button1.Click à à à Dim g Me.CreateGraphics à à à g.DrawString(About Visual Basic vbCrLf _ à à à and GDI vbCrLf A Great Team, _ à à à New Font(Times New Roman, 20), _ à à à Brushes.Firebrick, 0, 0) End Sub There are a couple of differences here. This is in theà Button1.Clickà event because whenà Form1à repaints itself in theà Loadà event, our graphics are lost. So we have to add them in a later event. If you code this, youll notice that the graphics are lost whenà Form1à has to be redrawn. (Mimimize and maximize again to see this.) Thats a big advantage to using the first method. Most references recommend using the first method since your graphics will be repainted automatically. GDI can be tricky!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.