如何在GDI +中旋转文本?

我想以特定角度显示给定的字符串。 我试图用System.Drawing.Font类来做这件事。 这是我的代码:

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

谁能帮我?

 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(); 

从这里开始 。

您可以使用RotateTransform方法( 请参阅MSDN )为Graphics上的所有绘图指定旋转(包括使用DrawString绘制的文本)。 angle以度为单位:

 graphics.RotateTransform(angle) 

如果您只想进行一次旋转操作,则可以通过使用负角度再次调用RotateTransform将变换重置为原始状态(或者,您可以使用ResetTransform ,但这将清除您应用的所有变换,这可能不是什么你要):

 graphics.RotateTransform(-angle) 

如果您想要一种方法在字符串中心位置绘制旋转的字符串,请尝试以下方法:

 public void drawRotatedText(Bitmap bmp, int x, int y, float angle, string text, Font font, Brush brush) { Graphics g = Graphics.FromImage(bmp); g.TranslateTransform(x, y); // Set rotation point g.RotateTransform(angle); // Rotate text g.TranslateTransform(-x, -y); // Reset translate transform SizeF size = g.MeasureString(text, font); // Get size of rotated text (bounding box) g.DrawString(text, font, brush, new PointF(x - size.Width / 2.0f, y - size.Height / 2.0f)); // Draw string centered in x, y g.ResetTransform(); // Only needed if you reuse the Graphics object for multiple calls to DrawString g.Dispose(); } 

最诚挚的问候Hans Milling ……