Drawing a Rotated Text to an Image in C#

后端 未结 2 791
旧时难觅i
旧时难觅i 2021-01-17 23:56

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

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

I\'m trying to

相关标签:
2条回答
  • 2021-01-18 00:15

    before g.DrawString(mytext, font, brush, 0, 0); use g.RotateTransform(45);

    0 讨论(0)
  • 2021-01-18 00:29

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

    From How to rotate Text in GDI+?

    0 讨论(0)
提交回复
热议问题