How to rotate text?

L

Leo

I would like to rotate text and draw it starting from certain
location, say (10, 20). The problem for me is that it does rotate, but
it never draws the text from (10, 20). Will you please tell me where I
am wrong?

double trangent = Math.Atan2(ptTo.Y - ptFrom.Y, ptTo.X - ptFrom.X);
float angle = (float)(trangent * (180/Math.PI));

SizeF stringSize = e.Graphics.MeasureString(Text, this.Font);
float midX = (ptTo.Y + ptFrom.Y) / 2.0F;
float midY = (ptTo.X + ptFrom.X) / 2.0F;
double xOrgText = midX - stringSize.Width * Math.Tan(trangent) /
2.0F;
double yOrgText = midY - stringSize.Width *
Math.Tan(Math.PI/-trangent) / 2.0F;

using (Pen blackPen = new Pen(Color.Black, 2))
{
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
{
e.Graphics.DrawLine(blackPen, ptFrom, ptTo);
e.Graphics.TranslateTransform((float)xOrgText, (float)yOrgText);
e.Graphics.RotateTransform(angle, MatrixOrder.Append);
e.Graphics.DrawString(Text, this.Font, blackBrush, (float)xOrgText, -
(float)yOrgText);
}
}

base.OnPaint(e);
 
J

John Baro

After you apply the rotation transform you will have to get the bounds of
the rotated text and move it back to your starting point of 10, 20.

You might need to do this with a path.

HTH
JB
 

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

Similar Threads


Top