(c) 2003 Visual Studio Magazine 
Fawcette Technical Publications

C#	Use Transformations on Your Drawings.
Listing 1	This function draws a simple house. The coordinate space's origin moves to the center of the window. Later, the function uses transforms to put the origin of the coordinate space at the center of each ellipse and rotates the ellipse.

private void OnPaintPanel(object sender, 
	System.Windows.Forms.PaintEventArgs e)
{
	// Erase the old window stuff:
	e.Graphics.FillRectangle(Brushes.White, 
		e.ClipRectangle);

	// Make the center of the window the origin, 
	// before applying any user transforms:

	e.Graphics.TranslateTransform 
		(panel2.Size.Width / 2, 
		panel2.Size.Height / 2);

	// Apply custom Transforms:
e.Graphics.MultiplyTransform (transforms);

	// Draw the house.
	e.Graphics.DrawRectangle( Pens.Black, -100, 
		-50, 200,100);
	Point [] triPoints = new Point [] { 
		new Point (-100, -50), 
		new Point (0, -100),
		new Point (100,-50)};
	e.Graphics.DrawPolygon (Pens.Black, 
		triPoints);

	// Draw a window rotated 30 degrees:
	// Move the center of the window (-60, -5) 
	// to the origin:
	e.Graphics.TranslateTransform(-60,-5);
	// Rotate:
	e.Graphics.RotateTransform( -30);
	// Draw the first window:
	e.Graphics.FillEllipse (Brushes.Black, -15, 
		-7.5F, 30,15);

	// Undo the rotate:
	e.Graphics.RotateTransform(30);
	//Move the center of the second window to the 
	// origin:
	e.Graphics.TranslateTransform (120, 0);

	// Rotate:
	e.Graphics.RotateTransform (30);
	// Draw:
	e.Graphics.FillEllipse(Brushes.Black, -15, 
		-7.5F, 30,15);
}

C#	Use a Transformed Graphics Object to Measure Distances 
Listing 2	You create the Graphics object, then apply your transforms. Then, you can use the Graphics object to convert easily between any of the different coordinate systems.

protected override void 
	OnMouseMove (MouseEventArgs e)
{

	if (Capture == true)
	{

		ControlPaint.DrawReversibleLine 
			(deviceStart, deviceFinish, 
			Color.White);
		// Change the finish point.
		PointF ptEnd = new PointF (e.X, e.Y);
		deviceFinish = new Point (e.X, e.Y);
		deviceFinish = 
			PointToScreen (deviceFinish);

		using (Graphics g = this.CreateGraphics ())
		{
			SetupGraphicsTransforms (g);
			worldFinish = ptEnd;

			PointF [] p = new PointF[1];
			p[0] = worldFinish;
			g.TransformPoints 
				(CoordinateSpace.World, 
				CoordinateSpace.Device, p);

			worldFinish= p[0];
			FinishPoint.Text = 
				worldFinish.ToString ();

		7	// Figure the distance:
			CalcAndDisplayDistance ();
			ControlPaint.DrawReversibleLine 
				(deviceStart, deviceFinish, 
				Color.White);
		}
	}
	base.OnMouseMove (e);
}

