Graphics.Transform Property question

  • Thread starter Thread starter John Rivers
  • Start date Start date
J

John Rivers

Hi

two things seem strange about this property
see the following working code:

// Graphics g;
Matrix tm = g.Transform.Clone();
tm.Translate(sin, cos, MatrixOrder.Prepend);
tm.Rotate(angle, MatrixOrder.Prepend);
tm.Translate(-box.Width, -box.Height, MatrixOrder.Prepend);
g.Transform = tm;

but this should be the equivalent:

g.Transform.Translate(sin, cos, MatrixOrder.Prepend);
g.Transform.Rotate(angle, MatrixOrder.Prepend);
g.Transform.Translate(box.Width, -box.Height, MatrixOrder.Prepend);

but this has a different outcome

can anybody see why?
 
Hi

two things seem strange about this property
see the following working code:

// Graphics g;
Matrix tm = g.Transform.Clone();
tm.Translate(sin, cos, MatrixOrder.Prepend);
tm.Rotate(angle, MatrixOrder.Prepend);
tm.Translate(-box.Width, -box.Height, MatrixOrder.Prepend);
g.Transform = tm;

but this should be the equivalent:

g.Transform.Translate(sin, cos, MatrixOrder.Prepend);
g.Transform.Rotate(angle, MatrixOrder.Prepend);
g.Transform.Translate(box.Width, -box.Height, MatrixOrder.Prepend);

but this has a different outcome

can anybody see why?

Yes. The matrix returned by the property is not the actual matrix being
used by the Graphics instance. It's a copy.

In your first example, there's no need to call Clone(). Just assign the
original Transform value, modify it, and assign it back. Alternatively,
used the Graphics class methods that affect the transformation directly,
rather than going through the matrix returned by the Transform property.

Pete
 
Thanks Peter

instead of using:

g.Transform.Rotate()

which is acting on a clone

I should be using:

g.TranslateTransform();
g.ScaleTransform();
g.RotateTransform();
 
Back
Top