Connecting Tech Pros Worldwide Forums | Help | Site Map

How to rotate text?

Leo
Guest
 
Posts: n/a
#1: Nov 16 '05
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);

John Baro
Guest
 
Posts: n/a
#2: Nov 16 '05

re: How to rotate text?


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
"Leo" <pkujchliu@yahoo.com> wrote in message
news:81553b16.0405301117.57c38470@posting.google.c om...[color=blue]
> 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);[/color]


Closed Thread


Similar C# / C Sharp bytes