2012-11-21 5 views
1

사용자 정의 배경에 RTF 텍스트를 그려야합니다. RTF 자체를 렌더링하려면 예 : here에 설명 된대로 확장 된 RichTextBox 컨트롤을 사용하고 있습니다. 그래픽이 화면과 연결되어 있으면 제대로 작동합니다. 그러나 비트 맵에서 생성 된 그래픽을 사용할 때 앤티 앨리어싱이 텍스트를 배경과 올바르게 혼합하지 않는 것처럼 클리어 타입 글꼴은 검은 색 조각이 있습니다 (배경을 먼저 그립니다). 이것의 이유는 무엇입니까? 그리고 어떻게 든 고칠 수 있습니까? 코드가 못생긴 비트 맵을 생산EM_FORMATRANGE 메시지를 사용하여 비트 맵에 RTF를 그리면 못생긴 검은 단편이있는 텍스트가 생성됩니다.

예 :

private void CreateBitmap(string rtf, Rectangle bitmapRectangle) 
{ 
    using (Bitmap bitmap = new Bitmap(bitmapRectangle.Width, bitmapRectangle.Height)) 
    { 
     using (Graphics gr = Graphics.FromImage(bitmap)) 
     { 
      gr.Clear(Color.Yellow); 

      // extended RichTextBox control from www.andrewvos.com 
      RichTextBoxDrawer rtbDrawer = new RichTextBoxDrawer(); 
      rtbDrawer.Rtf = rtf; 
      rtbDrawer.Draw(gr, bitmapRectangle); 

      bitmap.Save(@"c:\bitmap.png"); 
     } 
    } 
} 

한가지 더 : Graphics.DrawString 잘 작동하고 올바르게 앤티 앨리어싱 텍스트를 그립니다.

+1

이 변함 초기화하지 않는 부작용이다 비트 맵이 올바르게. "나는 처음으로 배경을 그려."당신이 그렇게 올바르게했다는 증거가 충분하지 않다. 스 니펫을 게시해야한다. –

+0

배경을 그리는 다소 복잡한 방법 외에도 간단한 Graphics.FillRectangle 또는 Graphics.Clear도 시도했지만 결과는 항상 동일했습니다. 비트 맵을 올바르게 초기화하려면이 방법이 충분합니까? – user1842413

답변

1

좋아, 내가 잘못된 장소에서 배경을 그리는 것처럼 보입니다. I는 디바이스 컨텍스트 핸들로부터 생성 된 그래픽 백그라운드를 그리는 경우 RichTextBoxDrawer.Draw 방법에서 전송 EM_FORMATRANGE 메시지 인, 그 텍스트가 정확하게 렌더링하기 :

public void Draw(Graphics graphics, Rectangle layoutArea, Bitmap background = null) 
{ 
    //Calculate the area to render. 
    SafeNativeMethods.RECT rectLayoutArea; 
    rectLayoutArea.Top = (int)(layoutArea.Top * anInch); 
    rectLayoutArea.Bottom = (int)(layoutArea.Bottom * anInch); 
    rectLayoutArea.Left = (int)(layoutArea.Left * anInch); 
    rectLayoutArea.Right = (int)(layoutArea.Right * anInch);  

    IntPtr hdc = graphics.GetHdc(); 
    using (Graphics backgroundGraphics = Graphics.FromHdc(hdc)) 
    { 
     // draw some background 
     backgroundGraphics.Clear(Color.Yellow); 
    } 

    // rest of the method is same 
}