The
Graphics
Class
The
Graphics
class is center of all
GDI+ classes. After
discussing graphics class, I'll discuss some
common GDI+
objects and their representation
and then we'll see some
sample applications
to apply this theory in our applications and
how it works.
The
Graphics class plays a vital role
in GDI+. No matter where you go, you go
through this class ;). The
Graphics class
encapsulates GDI+ drawing surfaces. Before
drawing any object (for example circle, or
rectangle ) we have to create a surface
using Graphics class.
There're
different of ways to get a
graphics object
in your application. You can either get a
graphics object on your form's paint event
or by overriding OnPaint() method of a form.
These both have one argument of type
System.Windows.Forms.PaintEventArgs. You
call its
Graphics
member to get the
graphics
object in your application. For example:
protected overrides sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
End Sub
Now you've the
Graphics object. Now you can do any thing
you want. The Graphics class has tons of
methods to drawing graphics objects such as
fonts, pens, lines,
path and
polygons,
images and
ellipse and so on. Some of the
graphics class members are described in the
following table -
DrawArc |
This
method draws an
arc. |
DrawBezier,
DrawBeziers,
DrawCurve |
These
methods draw a simple and bazier
curves. These curvers can be closed,
cubic and so on. |
DrawEllipse |
Draws
an ellipse or
circle. |
DrawImage |
Draws
an image. |
DrawLine |
Draws
a line. |
DrawPath |
Draws
the path (lines with
GraphicsPath ) |
DrawPie |
Draws
the outline of a pie section. |
DrawPolygon |
Draws
the outline of a
polygon. |
DrawRectangle |
Draws
the outline of a
rectangle. |
DrawString |
Draws
a string. |
FillEllipse |
Fills
the interior of an ellipse defined
by a bounding rectangle. |
FillPath |
Fills
the interior of a path. |
FillPie |
Fills
the interior of a pie section. |
FillPolygon |
Fills
the interior of a polygon defined by
an array of points. |
FillRectangle |
Fills
the interior of a rectangle with a
Brush |
FillRectangles |
Fills
the interiors of a series of
rectangles with a Brush |
FillRegion |
Fills
the interior of a Region. |
Now say, if you
want to draw an ellipse using the same
method we've described above, you override
OnPaint method and write the following code
-
protected overrides sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim pn As Pen = New Pen(Color.Green, 10)
g.DrawLine(pn, 100, 10, 30, 10)
g.DrawEllipse(New Pen(Color.Red, 20), 20, 40, 20, 20)
End Sub
That will draw
a ellipse. Before we see more samples, let's
discuss some GDI+ objects.
GDI+ Font,
Brush
and
Bitmap
GDI+ GraphicsPath
and
LinearGradientBrush
GDI+ Printing.
GDI+
and
DrawArc
and
DrawPath
GDI+ Color
and
ARGB
with
Example