The Graphics Class and Transformation

In Chapter 3 we saw that the Graphics class provides some transformation-related members. Before we move to other transformation-related classes, let's review the transformation functionality defined in the Graphics class, as described in Table 10.2. We will see how to use these members in the examples throughout this chapter.

The Transform property of the Graphics class represents the world transformation of a Graphics object. It is applied to all items of the object. For example, if you have a rectangle, an ellipse, and a line and set the Transform property of the Graphics object, it will be applied to all three items. The Transform property is a Matrix object. The following code snippet creates a Matrix object and sets the Transform property:

Table 10.2. Transformation-related members defined in the Graphics class

Member

Description

MultiplyTransform

Method that multiplies the world transformation of a Graphics object and a Matrix object. The Matrix object specifies the transformation action (scaling, rotation, or translation).

ResetTransform

Method that resets the world transformation matrix of a Graphics object to the identity matrix.

RotateTransform

Method that applies a specified rotation to the transformation matrix of a Graphics object.

ScaleTransform

Method that applies a specified scaling operation to the transformation matrix of a Graphics object by prepending it to the object's transformation matrix.

Transform

Property that represents the world transformation for a Graphics object. Both get and set.

TransformPoints

Method that transforms an array of points from one coordinate space to another using the current world and page transformations of a Graphics object.

TranslateClip

Method that translates the clipping region of a Graphics object by specified amounts in the horizontal and vertical directions.

TranslateTransform

Method that prepends the specified translation to the transformation matrix of a Graphics object.


 
Matrix X = new Matrix();
X.Scale(2, 2, MatrixOrder.Append);
g.Transform = X;

 

The transformation methods provided by the Graphics class are MultiplyTransform, ResetTransform, RotateTransform, ScaleTransform, TransformPoints, TranslateClip, and TranslateTransform. The MultiplyTransform method multiplies a transformation matrix by the world transformation coordinates of a Graphics object. It takes an argument of Matrix type. The second argument, which specifies the order of multiplication operation, is optional. The following code snippet creates a Matrix object with the Translate transformation. The MultiplyTransform method multiplies the Matrix object by the world coordinates of the Graphics object, translating all graphics items drawn by the Graphics object.


 
Matrix X = new Matrix();
X. Translate(200.0F, 100.0F);
g.MultiplyTransform(X, MatrixOrder.Append);

 

RotateTransform rotates the world transform by a specified angle. This method takes a floating point argument, which represents the rotation angle, and an optional second argument of MatrixOrder. The following code snippet rotates the world transformation of the Graphics object by 45 degrees:


 
g.RotateTransform(45.0F, MatrixOrder.Append);

 

The ScaleTransform method scales the world transformation in the specified x- and y-directions. The first and second arguments of this method are x- and y-direction scaling factors, and the third optional argument is MatrixOrder. The following code snippet scales the world transformation by 2 in the x-direction and by 3 in the y-direction:


 
g.ScaleTransform(2.0F, 3.0F, MatrixOrder.Append);

 

The TranslateClip method translates the clipping region in the horizontal and vertical directions. The first argument of this method represents the translation in the x-direction, and the second argument represents the translation in the y-direction:


 
e.Graphics.TranslateClip(20.0f, 10.0f);

 

The TranslateTransform method translates the world transformation by the specified x- and y-values and takes an optional third argument of MatrixOrder:


 
g.TranslateTransform(100.0F, 0.0F, MatrixOrder.Append);

 

We will use all of these methods in our examples.

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics

Transformation

Printing

Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET



GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
EAN: N/A
Year: 2003
Pages: 145

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net