Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate Text in GDI+?

Tags:

c#

gdi+

I want to display an given string in a specific angle. I tried to to do this with the System.Drawing.Font class. Here is my code:

Font boldFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold, GraphicsUnit.Pixel, 1, true);
graphics.DrawString("test", boldFont, textBrush, 0, 0);

Can anyone help me?

like image 982
eagle999 Avatar asked Dec 12 '10 11:12

eagle999


1 Answers

String theString = "45 Degree Rotated Text";
SizeF sz = e.Graphics.VisibleClipBounds.Size;
//Offset the coordinate system so that point (0, 0) is at the
center of the desired area.
e.Graphics.TranslateTransform(sz.Width / 2, sz.Height / 2);
//Rotate the Graphics object.
e.Graphics.RotateTransform(45);
sz = e.Graphics.MeasureString(theString, this.Font);
//Offset the Drawstring method so that the center of the string matches the center.
e.Graphics.DrawString(theString, this.Font, Brushes.Black, -(sz.Width/2), -(sz.Height/2));
//Reset the graphics object Transformations.
e.Graphics.ResetTransform();

Taken from here.

like image 142
Darin Dimitrov Avatar answered Oct 30 '22 11:10

Darin Dimitrov