如何在GDI +中旋转文本?

eag*_*999 12 c# gdi+

我想以特定角度显示给定的字符串.我试着在System.Drawing.Font课堂上这样做.这是我的代码:

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

谁能帮我?

Dar*_*rov 16

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();
Run Code Online (Sandbox Code Playgroud)

取自这里.


Tom*_*cek 10

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

graphics.RotateTransform(angle)
Run Code Online (Sandbox Code Playgroud)

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

graphics.RotateTransform(-angle)
Run Code Online (Sandbox Code Playgroud)

  • 完成绘制旋转文本后,@ eagle999使用ResetTransform() (2认同)

niv*_*978 9

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

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();
}
Run Code Online (Sandbox Code Playgroud)

最好的问候Hans Milling ......