Graphics States in GDI+ using VB.NET

In this article I will explain about understanding Graphics States in GDI+.
  • 3147
During the life cycle of a Graphics object, the object maintains a list of graphics states. These graphics states fall into various categories depending on the operations being applied to the Graphics object. For example, setting the compositing quality of a Graphics object changes the object's state.
Graphics states can be divided into three categories:
  1. Quality setting
  2. Transformations
  3. Clipping region
The first state of the Graphics object involves the quality of shapes and images. This state changes when you set the quality of a Graphics object using the SmoothingMode, TextRenderingHint, CompositingMode, CompositingQuality, and InterpolationMode properties of the Graphics class.
Transformation is another state that a Graphics object maintains. Transformation is the process of changing graphics object from one state to another by rotation, scaling, reflection, translation, and shearing.
The Graphics object maintains two transformation states: world and page. The world transformation defines the conversion of world coordinates to page coordinates. World coordinates are coordinates that you define in your program, and page coordinates are coordinates that GDI+ uses to expose the object coordinates. The page transformation defines the conversion of page coordinates to device coordinates. Device coordinates determine how a graphics object will be displayed on a particular display device.
The Graphics class provides the ScaleTransform, RotateTransform, and TranslateTransform methods, as well as the Transform property, to support transformations.
The world unit (by default) is always defined as a pixel. For example, in the following code snippet a rectangle will be drawn starting at 0 pixels from the left edge and 0 pixel from the top edge, with width and height of 100 and 50 pixels, respectively.

   Dim g As Graphics = Me.CreateGraphics()
    g.DrawRectangle(Pens.Green, 0, 0, 100, 50)

Page coordinates may be different from world coordinates, depending on the page unit and page scaling of the Graphics object. For example, if the page unit is an inch, the page coordinates will start at point (0, 0) but the width and height of the rectangle will be 100 inches and 50 inches, respectively.

TABLE 9.9: GraphicsUnit members




1/75 inch as the unit of measure.


The document unit (1/300 inch) as the unit of measure.


An inch as the unit of measure.


A millimeter as the unit of measure.


A pixel as the unit of measure.


A printer's point (1/72 inch) as the unit of measure.


The world unit as the unit of measure.

The PageScale and PageUnit properties define a page transformation. The PageUnit property defines the unit of measure used for page coordinates, and the PageScale property defines the scaling between world and page units for a Graphics object. The PageUnit property takes a value of type GraphicsUnit enumeration, which is defined in Table 9.9.

Listing 9.13 draws three ellipses with the same size but different PageUnit values: Pixel, Millimeter, and Point.
LISTING 9.13: Setting page transformation

    Private Sub TransformUnits_Click(ByVal sender As Object,ByVal e As System.EventArgs)
        ' Create a Graphics object and set its background as form's background
        Dim gAs Graphics = Me.CreateGraphics()

        ' Draw an ellipse with default units
        g.DrawEllipse(Pens.Red, 0, 0, 100, 50)

        ' Draw an ellipse with page unit as pixel
        g.PageUnit = GraphicsUnit.Pixel
        g.DrawEllipse(Pens.Red, 0, 0, 100, 50)

        ' Draw an ellipse with page unit as millimeter
        g.PageUnit = GraphicsUnit.Millimeter
        g.DrawEllipse(Pens.Blue, 0, 0, 100, 50)

        ' Draw an ellipse with page unit as point
        g.PageUnit = GraphicsUnit.Point
        g.DrawEllipse(Pens.Green, 0, 0, 100, 50)

        ' Dispose of object
   End Sub

Figure 9.21 shows the output from Listing 9.13. Although the parameters to DrawEllipse are the same,
we get results of different sizes because of the different PageUnit settings.

The third state of the Graphics object is the clipping region. A Graphics object maintains a clipping region that applies to all items drawn by that object. You can set the clipping region by calling the SetClip method. It has six overloaded forms, which vary in using a Graphics object, graphics path, region, rectangle, or handle to a GDI region as the first parameter. The second parameter in all six forms is CombineMode, which has six values: Complement, Exclude, Intersect, Replace, Union, and Xor. The Clip property of the Graphics object specifies a Region object that limits the portion of a Graphics object that is currently available for drawing. The ClipBounds property returns a RectangleF Structure that represents a bounding rectangle for the clipping region of a Graphics object.
FIGURE 9.21: Drawing with different PageUnit Values
Hope the article would have helped you in understanding Graphics States in GDI+. Read other articles on GDI+ on the website.


More Articles

© 2018 DotNetHeaven. All rights reserved.