绘制旋转文字到图像在C# [英] Drawing a Rotated Text to an Image in C#

查看:395
本文介绍了绘制旋转文字到图像在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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旋转文本+?

这篇关于绘制旋转文字到图像在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆