How can drawString method be used for writing diagonal way
Asked Answered
R

5

6

I am using c# 2005 i want to write string diagonally on image. But by default c# provides the option to write horizontally or vertically.

how we write diagonally?

Thanks

Runofthemill answered 18/9, 2008 at 10:39 Comment(0)
C
7

Do a Graphics.rotateTransform before the drawString call. Don't forget to reverse the change afterwards, as Phil Wright points out.

Cangue answered 18/9, 2008 at 10:42 Comment(1)
You can have lotsa fun adding a semi-random transform for every line in a text editor :)Dicast
H
9

You can use the RotateTransform and TranslateTransform that are available on the Graphics class. Because using DrawString is GDI+ the transforms affects the drawing. So use something like this...

g.RotateTransform(45f);
g.DrawString("My String"...);
g.RotateTransform(-45f);

Don't forget to reverse the change though!

Henrique answered 18/9, 2008 at 10:44 Comment(0)
C
7

Do a Graphics.rotateTransform before the drawString call. Don't forget to reverse the change afterwards, as Phil Wright points out.

Cangue answered 18/9, 2008 at 10:42 Comment(1)
You can have lotsa fun adding a semi-random transform for every line in a text editor :)Dicast
F
1

You can use this function.

   void DrawDigonalString(Graphics G, string S, Font F, Brush B, PointF P, int Angle)
   {
       SizeF MySize = G.MeasureString(S, F);
       G.TranslateTransform(P.X + MySize.Width / 2, P.Y + MySize.Height / 2);
       G.RotateTransform(Angle);
       G.DrawString(S, F, B, new PointF(-MySize.Width / 2, -MySize.Height / 2));
       G.RotateTransform(-Angle);
       G.TranslateTransform(-P.X - MySize.Width / 2, -P.Y- MySize.Height / 2);
   }

Like this

enter image description here

Frumpy answered 27/5, 2013 at 10:58 Comment(1)
Works like a charm! But how can I determine the width of the angled text? e.g. if I have a string width of 50. After rotating the text, it'll take less width...Capability
R
0

u have right..It can be done in that way..BUT text will be written from top to bottom always and I'm not sure u can change it from bottom to top.. cheers

Radiotelegraphy answered 29/3, 2010 at 14:13 Comment(0)
P
-1

There is another way to draw a text vertically which is built in the C#. There is no need of explicit graphics transformation. You can use the StringFormat class. Here is a sample code which draws a text vertically:

StringFormat sf = new StringFormat(); sf.FormatFlags = StringFormatFlags.DirectionVertical; e.Graphics.DrawString("My String", this.Font, Brushes.Black, PointF.Empty, sf);

Propylene answered 13/10, 2008 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.