Most drawing in .NET occurs in the context of a
Graphics object. (For those familiar with pre-.NET development in Windows, this is similar to a device context.)
Graphics objects provide a canvas on which you draw lines, shapes, bitmap images, and prerecorded drawing macros.
Graphics object do not contain the graphics surface itself; they are simply generic conduits to the actual canvas. There is always some surface behind the
Graphics object, whether it is a portion of the screen, a
Bitmap object, or the simulated surface of a printed page. Any drawing that is done to the
Graphics object immediately impacts the underlying surface.
Graphics object includes dozens of methods that let you draw shapes and images on the graphics surface, and perform other magical 2D activities. We'll cover many of them in this chapter.
Graphics object for an on-screen form or control is as easy as calling the form's or control's
Dim wholeFormGraphics As Graphics = _ Me.CreateGraphics( ) Dim buttonOnlyGraphics As Graphics = _ Button1.CreateGraphics( )
Some events, most notably the
Paint event for forms and controls, provide access to a
Graphics object through the event arguments.
Private Sub PictureBox1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) _ Handles PictureBox1.Paint Dim paintCanvas As Graphics =
You can also create a
Graphics object that is unrelated to ...