2011-12-19 7 views
0

私は文字列を描画し、それをカスタムアングルで回転させたいと思います。単純に、回転イメージを含む領域の新しい次元を計算し、次にビットマップオブジェクトを作成し、そのグラフィックスオブジェクトを使用して、3つの変換(中心、回転、および逆平行移動への変換)で文字列を描画します。私は次のコードを書いたが、品質は望ましくない。誰にもアイデアはありますか?C#で高品質の回転文字列を描画する方法は?

private Image RotateText(string Text, int FontSize, float Angle) 
    { 

     //Modify angle 
     Angle *= -1; 

     //Calculate rotation angle in radian 
     double AngleInRadian = (Angle * 2 * Math.PI)/360d; 

     //Instantiate a font for text 
     Font TextFont = new Font("Tahoma", FontSize, FontStyle.Bold); 

     //Measure size of the text 
     Graphics Graphic = this.CreateGraphics(); 
     SizeF TextSize = Graphic.MeasureString(Text, TextFont); 

     //Calculate size of the rotated text 
     double NewWidth = Math.Abs(TextSize.Width * Math.Cos(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Sin(AngleInRadian)); 
     double NewHeight = Math.Abs(TextSize.Width * Math.Sin(AngleInRadian)) + Math.Abs(TextSize.Height * Math.Cos(AngleInRadian)); 

     //Instantiate a new image for rotated text 
     Bitmap RotatedText = new Bitmap((int)(Math.Round(NewWidth)), (int)(Math.Round(NewHeight))); 

     //Get graphic object of new isntantiated image for painting 
     Graphics TextGraphic = Graphics.FromImage(RotatedText); 
     TextGraphic.InterpolationMode = InterpolationMode.High; 

     //Calcaute coordination of center of the image 
     float OX = (float)NewWidth/2f; 
     float OY = (float)NewHeight/2f; 

     //Apply transformations (translation, rotation, reverse translation) 
     TextGraphic.TranslateTransform(OX, OY); 
     TextGraphic.RotateTransform(Angle); 
     TextGraphic.TranslateTransform(-OX, -OY); 

     //Calculate the loaction of drawing text 
     float X = (RotatedText.Width - TextSize.Width)/2f; 
     float Y = (RotatedText.Height - TextSize.Height)/2f; 

     //Draw the string 
     TextGraphic.DrawString(Text, TextFont, Brushes.White, X, Y); 

     //Return the image of rotated text 
     return RotatedText; 

    } 

結果がこれです:

enter image description here

+0

//回転したテキストのサイズを計算 ダブルNewWidth = Math.Abs​​(TextSize.Width * Math.Cos(AngleInRadian))+ Math.Abs​​を取りますTextSize.Height * Math.Sin(AngleInRadian)); double NewHeight = Math.Abs​​(TextSize.Width * Math.Sin(AngleInRadian))+ Math.Abs​​(TextSize.Height * Math.Cos(AngleInRadian));あなたのコードでこの計算は正しいですか? – Kira

+0

それは//回転したテキストのサイズを計算します double NewWidth = Math.Abs​​(TextSize.Width * Math.Cos(AngleInRadian)); double NewHeight = Math.Abs​​(TextSize.Width * Math.Sin(AngleInRadian)); – Kira

+0

今、私は回転したテキストの正確な幅と高さを計算したいと考えています。私は計算http://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.htmlのための次のリンクをチェックした。上記のリンクをチェックすることは私の2番目のコメントの数式を示す。しかし、ほとんどのデベロッパーがコードを使用しています(私の最初のコメントで)。だから、私はちょうどどちらが正しいか知りたいだけです。 – Kira

答えて

2

あなたはいくつかのことを試すことができますAntiAlias

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.textrenderinghint.aspx

+0

これは、ビットマップにキャプチャして回転するときは機能しません。ビットマップをある角度でレンダリングするときにヒントはありません –

+1

実際には、ここでは起こっていません。コードに従うと、 'RotatedText'ビットマップが実際に回転前のテキストを保持していることがわかります。ビットマップは回転しません。テキストは既にビットマップ上で回転しています。 TextRenderingは違いを生み出します。また、ケースがあなたの言うとおりであった場合、結果は結果の画像のようにはなりません。 –

0

にごGraphicsオブジェクトのTextRenderingHintプロパティを設定してみてください:

あなたのXAML 0
  • TextOptions.TextFormattingMode = "表示"(および他のTextOptions添付プロパティ)あなたのC#の

  • TextOptions.SetTextFormattingMode(この、TextFormattingMode.Display );

  • (SO話題this

+0

質問は、System.Drawingを使ってレンダリングすることです。WPFについてではありません。 –

+0

@atornbladあなたが言ったように、問題はレンダリングに関するもので、WPFを使用してレンダリングします。彼はC#コードで同じプロパティを使うことができます。 (私はC#の使用法を表示するように編集します) –

+0

いいえ、彼は 'Graphics.DrawString'を使ってテキストをレンダリングします。 –

関連する問題