The Graphics Class


This article has been excerpted from book "Graphics Programming with GDI+ ".

Graphics objects are the heart of GDI+. They are represented by the Graphics class, which defines methods and properties to draw and fill graphics objects. Whenever an application needs to draw or paint something, it has to use the Graphics object. Hence, understanding the Graphics class, its methods, and its properties is very important.

Specifically, in this article we will discuss the methods and properties of the Graphics class, and how to use them in real-world applications, including line charts, pie charts, and our GDI+Painter application. GDI+Painter is similar to the PaintBrush application, which allows you to draw simple graphics object such as lines, rectangles, and circles and save the images as bitmaps.

Graphics Class Properties

The Graphics class provides a long list of properties (see Table 3.1) and methods.

TABLE 3.1: Graphics properties

Property Description

Clip

Gets and sets a Region type that limits the drawing region of the Graphics object.

ClipBounds

Returns a RectangeF structure that bounds the clipping region of this Graphics object. Supports read only access.

CompositingMode

Returns a value of type CompositingMode enumeration representing how composite images are drawn to the Graphics object.

CompositingQuality

Gets and sets the rendering quality (directly proportional to the visual quality of the output and inversely proportional to the rendering time) of composite images, represented by the CompositingQuality enumeration.

DpiX

Returns the horizontal resolution (dots per inch) of a Graphics object.

DpiY

Returns the vertical resolution (dots per inch) of a Graphics object.

InterpolationMode

Gets and sets the interpolation mode (which determines intermediate values between two endpoints), represented by the InterpolationMode enumerator.

IsClipEmpty

Returns a value indicating whether the clipping region of a Graphics object is empty. When there is no clipping this property return false.

IsVisibleClipEmpty

Returns a value indicating whether the visible clipping region of a Graphics object is empty.

PageScale

Gets and sets a value for scaling between world units and page units for this Graphics object.

PageUnit

Gets and sets a value that represents the unit of measure for page coordinates.

PixelOffsetMode

Gets and sets a value for the pixel offset mode (PixelOffsetMode enumeration).

RenderingOrigin

Represents the rendering origin of a Graphics object for dithering and hatch brushes.

SmoothingMode

Gets and sets the smoothing mode of a Graphics object (SmoothingMode enumeration). Does not affect text. Smoothing modes include high quality, high speed, and anti-aliasing.

TextContrast

Gets and sets the gamma correction value for rendering anti-aliased and ClearType text values, ranging from 0 to 12. The default is 4.

TextRenderingHint

Gets and sets the text rendering quality (TextRenderingHint enumeration). Affects only text drawn on the Graphics object.

Transform

Gets and sets the world transformation matrix (transformation is the process of converting graphics objects from one state to another). The transformation state is represented by a transformation matrix.

VisibleClipBounds

Gets and sets the visible clipping region of the Graphics object (the intersection of the clipping region of the Graphics object and the clipping region of the window).


Graphics Class Methods

We can divide Graphics class methods into three categories: draw, fill, and miscellaneous. Draw methods are used to draw lines, curves, and outer boundaries of closed curves and images. Fill methods fill the interior area of graphics objects. There are also a few miscellaneous methods that fall in neither category-for example, MeasureString and Clear.

Draw Methods

The draw methods of the Graphics class are used to draw lines, curves, and outer boundaries of closed curves and images. Table 3.2 lists the draw methods of the Graphics class.

TABLE 3.2: Graphics draw methods


Mehtods Description

DrawArc

Draws an arc (a portions of an ellipse specified by a pair of coordinates, a width, a height, and start and end angles).

DrawBezier

Draws a Bezier curve defined by four Point structures.

DrawBeziers

Draws a series of Bezier splines from an array of Point structures.

DrawClosedCurve

Draws a closed cardinal spline defined by an array of Point structures.

DrawCurve

Draws a cardinal spline through a specified array of Point structures.

DrawEllipse

Draws an ellipse defined by a bounding rectangle specified by a pair of coordinates, a height, and a width.

DrawIcon Draws an image represented by the specified Icon object at the specified coordinates.

DrawIconUnstretched

Draws an image represented by the specified Icon object without scaling the image.

DrawImage

Draws the specified Image object at the specified location and with the original size.

DrawImageUnscaled

Draws the specified Image object with its original size at the location specified by a coordinate pair.

DrawLine

Draws a line connecting two points specified by coordinate pairs.

DrawLines

Draws a series of line segments that connect an array of Point structures.

DrawPath

Draws a GraphicsPath object.

DrawPie

Draws a pie shape specified by a coordinate pair, a width, a height, and two radial lines.

DrawPolygon

Draws a polygon defined by an array of Point structures.

DrawRectangle

Draws a rectangle specified by a coordinate pair, a width, and a height.

DrawRectangles

Draws a series of rectangles specified by an array of Rectangle structures.

DrawString

Draws the specified text string at the specified location using the specified Brush and Font objects.


Conclusion

Hope the article would have helped you in understanding Graphic Class methods and properties. Read other articles on GDI+ on the website.


bookGDI.jpg This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all