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( _
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
Click the Back button on your browser to return
--------
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( _
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
The next page demonstrates the CreateGraphics method ...

