C# rotate a string 180 degrees
Hi im having a few problems rotati开发者_高级运维ng a string, i found that you need graphics.rotate() but when i change the rotation, i cannot even see the string. It appears the pivot point has completely thrown me. Also i saw an example with transform but i decided i did not need this?
If my string was a graph label, reading top to bottom and i needed to rotate it 180 degrees so that it read bottom to top, how would i do this?
Thanks in advance
A string is a data type, like for a variable. I assume you mean you want to rotate text displayed on a Form or Webpage. What type of application is it?
Also, please specify in what type of control the string is displayed... textbox, label, gridview...
The graphics.Rotate(180) method uses the location as pivot point. So basically, your string is thrown off the clip and is not rendered at it's supposed location. If you're only rendering this string in the clip, it should be easy enough to replace at the right location.
Here's a temporary solution, assuming you're drawing inside a picture box and you're only rendering the string inside of it.
g.TranslateTransform(pictureBox1.Width, pictureBox1.Height);
g.RotateTransform(180);
You can use the rotation matrix :
cos(alpha) sin(alpha);
-sin(alpha) cos(alpha)
If alph = 90
we obtain this matrix
0 1;
-1 0
So the new calculate point being : Xnew = Yold and Ynew = - Xold
For example,
g.DrawString(str, new Font("Arial", 16), new SolidBrush(Color.Black), 50, 25);
g.RotateTransform(90);
g.DrawString(str, new Font("Arial", 16), new SolidBrush(Color.Black),25+8, -50-8);
g.ResetTransform();
an other exemple whith the angle 180°
Xnew = cons(alpha) * Xold +sin(alpha) Yold
Yold = -sin(alpha) * Xold + cons(alpha) Yold
Xnew = - Xold and Ynew = -Yold
g.RotateTransform(180);
g.DrawString(str, new Font("Arial", 16), new SolidBrush(Color.Black), -50-8 , -25-8);
g.ResetTransform();
which 8 is size of the string font divided by 2.
regards,
Maybe try rotating only a few degrees to start with and see where the graphics are starting to shift? The 180 might be making it render "off screen".
If you are using Win32/GDI/GDI+ then see my answer to this question:
How to draw vertical text in Windows GUI?
If you are using HTML or some other web based solution then these might help:
https://stackoverflow.com/search?q=vertical+text
If you do not mean vertical text and do in fact really mean a rotated string then you need to use a transform and drawstring.
Graphics.RotateTransform(90);
Graphics.DrawString("123456", this.Font, new SolidBrush(Color.Black), 0, 0);
Of course you may need a translation transform to bring the text back into view.
精彩评论