2011-12-19 3 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 계산을 위해 다음 링크를 확인했다. 위의 링크를 확인하는 것은 나의 두 번째 코멘트에서 수식을 보여준다. 하지만 대부분의 개발자는 내 첫 번째 의견에서 코드를 사용하고 있습니다. 그래서 어느 것이 옳은지 궁금합니다. – 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);

  • (A SO 주제 this

+0

질문은 WPF가 아닌 System.Drawing을 사용하여 렌더링하는 것에 관한 것입니다. –

+0

@atornblad 당신이 말했듯이 문제는 렌더링에 관한 것이고 그는 WPF를 사용하여 렌더링합니다. 그는 C# 코드에서 첨부 된 속성을 그대로 사용할 수 있습니다. (C# 사용법을 보여주기 위해 편집합니다.) –

+0

아니요, 그는'Graphics.DrawString'을 사용하여 텍스트를 렌더링합니다. –

관련 문제