문자열을 그리고 사용자 정의 각도로 회전시키고 싶습니다. 간단히 말하면, 회전 된 이미지를 포함하는 영역의 새로운 차원을 계산 한 다음 비트 맵 객체를 만들고 그 그래픽 객체를 사용하여 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 계산을 위해 다음 링크를 확인했다. 위의 링크를 확인하는 것은 나의 두 번째 코멘트에서 수식을 보여준다. 하지만 대부분의 개발자는 내 첫 번째 의견에서 코드를 사용하고 있습니다. 그래서 어느 것이 옳은지 궁금합니다. – Kira