Technology Microsoft Software & solutions

GDI+ Graphics in Visual Basic 2005 .NET

< Continued from page 1

The file we will create will have the commands for the basic red-blue blended "background" that was used in Part 4 of the tutorial for a "temperature guage" display. The code to create our Metafile file is in a simple Click event for a Button named LoadMetafile:

Private Sub LoadMetafile_Click( ...
   Using g As Graphics = Me.CreateGraphics
      Dim hdc As IntPtr = g.GetHdc
      Using mf As Metafile = _
         New Metafile("D:\myMeta.emf", hdc)
         Using mg As Graphics = Graphics.FromImage(mf)
            Using myBrush As LinearGradientBrush _
               = New LinearGradientBrush( _
               Me.ClientRectangle, _
               Color.Blue, Color.Red, _
               LinearGradientMode.Horizontal)
               mg.FillRectangle( _
                  myBrush, Me.ClientRectangle)
            End Using
         End Using
         g.ReleaseHdc(hdc)
      End Using
   End Using
End Sub



The result is shown in the illustration below:



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

The first line creates a Graphics object using the CreateGraphics method for the form (Me). We've seen this before.

But the second line is new. Visual Basic programmers with no experience with a language like C or C++ might not understand what's happening here. An IntPtr object is usually called a "handle". Handles are just integers that "point to" a specific location in memory and the C family of languages use them a lot to reference objects in code. Visual Basic, however, has traditionally not used them at all. An honest C programmer will tell you that pointer bugs are one of the biggest sources of frustration and confusion. The name hdc is traditional too. It's often capitalized as hDC which stands for "handle to the Device Context". Device Context is a very fundamental part of Windows programming and is the structure in Windows that defines the attributes of text and images GDI+ paints.

Since VB.NET will be using the results provided by a Graphics object and placing them in a file, the Metafile object gets them directly from the Device Context. The next line creates the Metafile object and passes it the two items of information that it needs:
  • The file to create
  • The handle to the Graphics object

Now we create a new Graphics object using the Metafile object as an image. This is the Graphics object that will actually be used to paint the shapes. But the results go into a file instead of on the screen. From this point on down to the statement that releases the handle, it's standard GDI+ commands with the only difference that the mgGraphics object is used instead of the gGraphics object. The only one thread can use a Device Context handle at a time, so the ReleaseHdc method has to be called to release the handle when it's no longer needed. Recording to the file stops when the Graphics object is deleted or goes out of scope.

The code to actually use the Metafile file is quite a bit simpler:

Private Sub DisplayMetafile_Click( ...
   Using g As Graphics = Me.CreateGraphics
      Using mf As Metafile = New Metafile("D:\myMeta.emf")
         g.DrawImage(mf, New Point(0, 0))
      End Using
   End Using
End Sub


Or ... you can use several page example provided by Microsoft here. I have often complained that Microsoft sometimes provides examples that seem to have the single goal of confusing you.


Leave a reply