私は文字列を描画し、それをカスタムアングルで回転させたいと思います。単純に、回転イメージを含む領域の新しい次元を計算し、次にビットマップオブジェクトを作成し、そのグラフィックスオブジェクトを使用して、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;
}
結果がこれです:
//回転したテキストのサイズを計算 ダブル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
それは//回転したテキストのサイズを計算します double NewWidth = Math.Abs(TextSize.Width * Math.Cos(AngleInRadian)); double NewHeight = Math.Abs(TextSize.Width * Math.Sin(AngleInRadian)); – Kira
今、私は回転したテキストの正確な幅と高さを計算したいと考えています。私は計算http://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.htmlのための次のリンクをチェックした。上記のリンクをチェックすることは私の2番目のコメントの数式を示す。しかし、ほとんどのデベロッパーがコードを使用しています(私の最初のコメントで)。だから、私はちょうどどちらが正しいか知りたいだけです。 – Kira