Transformation with Pens

Transformation is the process of changing graphics objects from one state to another. Rotation, scaling, reflection, translation, and shearing are examples of transformation.

The Pen class provides methods for transformation and rotation. The RotateTransform method rotates a transformation by an angle. This method takes a rotation angle of type float. The second argument, MatrixOrder, is an optional parameter that provides an order for matrix transformation operations. The MatrixOrder enumeration defines the matrix order, which has two members: Append and Prepend. The matrix order is the order in which a matrix is multiplied with other matrices.

The difference between Append and Prepend is the order of the operation. For example, if two operations are participating in a process, the second operation will be performed after the first when the matrix order is Append; when the order is Prepend, the second operation will be performed before the first.

The MultiplyTransform method multiplies a transformation matrix by a pen. Its first argument is a Matrix object, and the optional second argument is the matrix order of type MatrixOrder enumeration.

Note

The Matrix class is discussed in more detail in Chapter 10.

The TranslateTransform method of the Pen class translates a transformation by the specified dimension. This method takes two float type values for translation in x and y, and an optional third parameter of type MatrixOrder.

Listing 4.22 uses the ScaleTransform and RotateTransform methods to apply rotation on pens and rectangles.

Listing 4.22 Applying transformation on pens

private void menuItem5_Click(object sender,
 System.EventArgs e)
{
 Graphics g = this.CreateGraphics();
 g.Clear(this.BackColor);
 // Create a Pen object
 Pen bluePen = new Pen(Color.Blue, 10);
 Pen redPen = new Pen(Color.Red, 5);
 // Apply rotate and scale transformations
 bluePen.ScaleTransform(3, 1);
 g.DrawEllipse(bluePen, 20, 20, 100, 50);
 g.DrawRectangle(redPen, 20, 120, 100, 50);
 bluePen.RotateTransform(90, MatrixOrder.Append);
 redPen.ScaleTransform(4, 2, MatrixOrder.Append);
 g.DrawEllipse(bluePen, 220, 20, 100, 50);
 g.DrawRectangle(redPen, 220, 120, 100, 50);
 // Dispose of objects
 redPen.Dispose();
 bluePen.Dispose();
 g.Dispose();
}

Figure 4.27 shows the output from Listing 4.22. The first ellipse and rectangle are drawn normally. The second ellipse and rectangle are drawn after rotation and scaling have been applied to their pens.

Figure 4.27. Rotation and scaling

graphics/04fig27.jpg

Chapter 10 discusses rotation, scaling, and other transformation methods in more detail.

Note

You need to reference the System.Drawing.Drawing2D namespace in order to run the code in the listings of this section because the Matrix class and the MatrixOrder enumeration are defined in this namespace.


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