2011-09-20 3 views
1

다음 코드를 사용하여 C# 코드를 사용하여 텍스트가있는 그림을 만듭니다.왜 System.Drawing + ClearType 글꼴에 검은 색 조각이 있습니까?

   // Create font. Parameter is a global variable 
     Font objFont = new Font(fontname, fontsize, fontstyle, System.Drawing.GraphicsUnit.Pixel); 

     // Grab an existing image from picture box. (target is picturebox's name) 
     Bitmap result; 
     if (target.Image != null) 
     { 
      result = new Bitmap(target.Image); 
     } 
     else 
     { 
      result = new Bitmap(target.Width, target.Height); 
     } 
     Graphics objGraphics = Graphics.FromImage(result); 

     // And draw to it. Select a mode with check box. 

     objGraphics.SmoothingMode = SmoothingMode.HighQuality; 
     if (!checkBox1.Checked) 
     { 
      objGraphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 
     } 
     else 
     { 
      objGraphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit; 
     } 
     Brush b = new LinearGradientBrush(new Rectangle(new Point(x, y), objGraphics.MeasureString(text, objFont).ToSize()),color1,color2,LinearGradientMode.Vertical); 

     objGraphics.DrawString(text, objFont, b, x, y); 
     objGraphics.Save(); 

     //Set the result to picturebox 

     target.Image = result; 

     objGraphics.Dispose(); 
     b.Dispose(); 

이 코드보다 먼저 target.BackColor가

target.BackColor = Color.Black; 

와 같은 원하는 색상으로 설정되었습니다. 결과 :

http://image.free.in.th/z/ie/yqbsg.png

왜 ClearType 글꼴이 밝은 bg에서 그렇게 못 생겼는지 궁금 해서요? (어두운 보라색과 같은 bg에서는 검정 테두리가 보이지 않지만 여전히 그렇습니다)

+0

GDI와 ClearType은 WinForms 응용 프로그램에서 잘 섞이지 않습니다. WinForms는 System.Drawing 이후로 사용하고 있다고 가정합니다. – BoltClock

답변

7
else 
    { 
     result = new Bitmap(target.Width, target.Height); 
    } 

, 당신은 비트 맵의 ​​픽셀을 초기화하지 않은 (어두운 보라색처럼 BG에서 당신은 검은 색 테두리를 통지하지 않습니다하지만 여전히있다). 기본값은 Color.Transparent입니다. Color.Transparent는 빨강, 녹색 및 파랑 색이 0이므로 검정색으로 앤티 앨리어스가 적용됩니다. 분홍색 배경에 비트 맵을 표시하면 앤티 앨리어싱 픽셀이 혼합되어 그려지지 않기 때문에 매우 잘 보입니다 분홍색 배경으로. 그들은 단지 검은 배경에 잘 어울립니다.

Graphics.Clear()를 사용해야합니다. 투명성을 의도한다면 앤티 앨리어싱을 포기하십시오.

+0

플러스 1 좋은 캐치 – MStp

관련 문제