I'm using the method posted here Using a matrix to rotate rectangles individually
(I hope this is not a repost, if it is maybe i'm doing something wrong)
I'm using the perfectly defined RotateRectangle method as follows
public void RotateRectangle(Graphics g, Rectangle r, float angle)
{
using (Matrix m = new Matrix())
{
m.RotateAt(angle, new PointF(r.Left + (r.Width / 2),
r.Top + (r.Height / 2)));
g.Transform = m;
g.DrawRectangle(Pens.Black, r);
g.ResetTransform();
}
}
And then on my drawing i create a label, and a rectangle outside of it
g.DrawString("e/10", fnt2, new SolidBrush(Color.Black), (int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25); //15 para tras, 15 para cima
Rectangle r1 = new Rectangle((int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25, 25, 12);
RotateRectangle(g, r1, 40);
(Notice the rectangle in top left corner) Am I missing something? Or should I go eat bananas?
Your RotateRectangle
is not rotating a rectangle of canvas; it's drawing a rotated rectangle.
Everything that's drawn between setting the Transform
and the ResetTransform
will be drawn rotated.
Therefore, if you want the DrawString
to be rotated, you need to put it after you set the Transform, and before you reset it.
using (Matrix m = new Matrix())
{
m.RotateAt(angle, new PointF(r.Left + (r.Width / 2),
r.Top + (r.Height / 2)));
g.Transform = m;
g.DrawString("e/10", fnt2, new SolidBrush(Color.Black), (int)(ecobdesenho / 10) / 2 + esp - 15, esp - 25); //15 para tras, 15 para cima
g.DrawRectangle(Pens.Black, r);
g.ResetTransform();
}