2011-09-20 45 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); 
    } 

這是一個問題,你有沒有初始化位圖的像素。他們將默認爲Color.Transparent。由於Color.Transparent在0處具有紅色,綠色和藍色,所以會導致文本被消除鋸齒爲黑色。然後,當您在粉紅色背景下顯示位圖時,抗鋸齒像素變得非常明顯,因爲它們未被繪製爲混合變成粉紅色的背景。他們只在黑色背景上看起來不錯。

您需要使用Graphics.Clear()。或者如果透明度是打算放棄反走樣。

+0

加1個漂亮的接球 – MStp