绘制旋转文字到图像在C#图像、文字

2023-09-03 01:25:56 作者:一人独守一城

我使用的是图形类的束带方法绘制的图像的字符串。

I'm using the the drawstring method of Graphics class to draw a String on Image.

  g.DrawString(mytext, font, brush, 0, 0);

我试图用旋转的旋转变换图形对象的功能,使文本可以在任何angle.How我能做到这一点使用旋转变换绘制由角文本。 该旋转变换code我用的是

I'm trying to rotate the text by angle using the Rotate Transform Function of the graphic object so that the text can be drawn at any angle.How can i do it using Rotate Transform. The rotate Transform Code i used is

    Bitmap m = new Bitmap(pictureBox1.Image);
    Graphics x=Graphics.FromImage(m);
    x.RotateTransform(30);
    SolidBrush brush = new SolidBrush(Color.Red);
    x.DrawString("hi", font,brush,image.Width/2,image.Height/2);
//image=picturebox1.image
    pictureBox1.Image = m;

文字是画在一个旋转的角度,但它不是画在中间,我want.Plz帮助我。

The Text is Drawn at a rotated angle but it is not drawn at the centre as i want.Plz help me out.

推荐答案

这是不够的,只是 RotateTransform TranslateTranform 如果要居中的文本。您需要抵消文本的起点,也通过测量:

It's not enough to just RotateTransform or TranslateTranform if you want to center the text. You need to offset the starting point of the text, too, by measuring it:

Bitmap bmp = new Bitmap(pictureBox1.Image);
using (Graphics g = Graphics.FromImage(bmp)) {
  g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
  g.RotateTransform(30);
  SizeF textSize = g.MeasureString("hi", font);
  g.DrawString("hi", font, Brushes.Red, -(textSize.Width / 2), -(textSize.Height / 2));
}

从如何GDI旋转文本+?

 
精彩推荐