2012-01-05 8 views
1

C# 양식 프로젝트에서 다음 코드를 작성하여 원하는대로 표시 할 수 있지만 융합을 시도하는 두 개의 다른 "세계"가있는 것처럼 보입니다.FormattedText를 사용하여 비트 맵 만들기

FormattedText text = new FormattedText(textBox1.Text, CultureInfo.GetCultureInfo("en-us"), System.Windows.FlowDirection.LeftToRight, new Typeface("Tahoma"), 20, System.Windows.Media.Brushes.Black); 
text.MaxTextWidth = 480; 
text.MaxTextHeight = 480; 
DrawingVisual d = new DrawingVisual(); 
DrawingContext d1 = d.RenderOpen(); 
d1.DrawText(text, new System.Windows.Point(0, 0)); 
d1.Close(); 
RenderTargetBitmap bmp = new RenderTargetBitmap(480, 480, 120, 96, PixelFormats.Pbgra32); 
bmp.Render(d); 
System.Windows.Controls.Image I=new System.Windows.Controls.Image(); 
I.Source = bmp; 

나에게 Windows.Media.ImageSource을 가져옵니다. System.Drawing 네임 스페이스를 사용하기 위해 모든 것을 마이그레이션하고 싶습니다.

기본적으로 위의 코드가 작동하도록 WPF 라이브러리를 가져와야했기 때문에 기본적인 작업이므로 Windows Forms에서 어떻게하면 좋을지 비추어 볼 수 있습니다.

참고 : 정말하고 싶습니다. 줄 바꿈을 허용하는 방식으로 비트 맵에 텍스트를 그려서 비트 맵으로 조작하고 싶습니다. Windows Forms에서이 작업을 수행하는 간단한 방법이 있다면 더 좋지 않을지라도 제대로 작동합니다.

답변

3

예, WPF 코드입니다. 완전히 다른 세계입니다.

var bmp = new Bitmap(480, 480); 
using (var gr = Graphics.FromImage(bmp)) { 
    gr.Clear(Color.White); 
    TextRenderer.DrawText(gr, textBox1.Text, this.Font, 
     new Rectangle(0, 0, bmp.Width, bmp.Height), 
     Color.Black, Color.White, 
     TextFormatFlags.WordBreak | TextFormatFlags.Left); 
} 
if (pictureBox1.Image != null) pictureBox1.Image.Dispose(); 
pictureBox1.Image = bmp; 

I 양식에 그림 상자에서 짐작 다음 System.Drawing 버전은 다음과 같이 유사해야한다.

관련 문제