Graphics.Transform Property question

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?
 
P

Peter Duniho

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
 
J

John Rivers

Thanks Peter

instead of using:

g.Transform.Rotate()

which is acting on a clone

I should be using:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top