2011-11-01 69 views
8

我正在使用Graphics類的drawstring方法在圖像上繪製字符串。將旋轉文字繪製到C中的圖像中#

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

我試圖用旋轉的旋轉變換圖形對象的功能使得文本能夠在任何angle.How我能做到這一點使用旋轉變換繪製的角度文本。 我使用了旋轉變換守則

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幫助我。這是不夠的

+1

我們展示旋轉變換,你嘗試過的代碼。 – mbeckish

+0

你能告訴我們整個方法(例如,這是OnPaint事件)嗎?另外,你能告訴我們你得到的結果嗎? – mbeckish

回答

22

只是RotateTransformTranslateTranform如果要居中的文本。您需要抵消文本的起點,也通過測量:

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

How to rotate Text in GDI+?

+0

@ LarsTech.Works就像一個Charm.Can你可以幫我在image.I添加代碼請參閱。 – techno

2

g.DrawString(mytext, font, brush, 0, 0);使用g.RotateTransform(45);

+0

我已經使用這種方法。Plz看到編輯。 – techno