2013-07-16 21 views
2

我正在尋找一種文本渲染方法,其中圖形對象從位圖初始化,文本被繪製在其上,文本看起來像附加圖像中的第一行。不同的文本渲染方法不生成我想要的一個

有人可以請解釋一種方法,將做到這一點?我不完全理解爲什麼沒有下面的方法可以重現:在測試

字體是:瀨越UI,8.25pt

protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 
{ 
    base.OnPaint(e); 

    //drawing the string with the Graphics object the form gives us 
    e.Graphics.DrawString("1. This is a test using DrawString. " + e.Graphics.TextRenderingHint.ToString(), 
          base.Font, Brushes.Black, new Point(10, 10)); 

    //width used for all images 
    const int width = 300; 

    //drawing the string with Graphics objects initialized from bitmaps 
    Bitmap bmp = new Bitmap(width, 20); 
    using (Graphics gfx = Graphics.FromImage(bmp)) 
    { 
     gfx.PageUnit = GraphicsUnit.Pixel; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     gfx.DrawString("2. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(), 
         base.Font, Brushes.Black, Point.Empty); 
    } 
    e.Graphics.DrawImage(bmp, new Point(10, 30)); 

    bmp.Dispose(); 
    bmp = new Bitmap(width, 20); 

    using (Graphics gfx = Graphics.FromImage(bmp)) 
    { 
     gfx.PageUnit = GraphicsUnit.Pixel; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; 
     gfx.DrawString("3. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(), 
         base.Font, Brushes.Black, Point.Empty); 
    } 

    e.Graphics.DrawImage(bmp, new Point(10, 50)); 

    bmp.Dispose(); 
    bmp = new Bitmap(width, 20); 

    using (Graphics gfx = Graphics.FromImage(bmp)) 
    { 
     gfx.PageUnit = GraphicsUnit.Pixel; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; 
     gfx.DrawString("4. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(), 
         base.Font, Brushes.Black, Point.Empty); 
    } 

    e.Graphics.DrawImage(bmp, new Point(10, 70)); 

    bmp.Dispose(); 
    bmp = new Bitmap(width, 20); 

    using (Graphics gfx = Graphics.FromImage(bmp)) 
    { 
     gfx.PageUnit = GraphicsUnit.Pixel; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
     gfx.DrawString("5. This is a test using DrawString. " + gfx.TextRenderingHint.ToString(), 
         base.Font, Brushes.Black, Point.Empty); 
    } 

    e.Graphics.DrawImage(bmp, new Point(10, 90)); 

    bmp.Dispose(); 
    bmp = new Bitmap(width, 20); 

    using (Graphics gfx = Graphics.FromImage(bmp)) 
    { 
     gfx.PageUnit = GraphicsUnit.Pixel; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     using (GraphicsPath path = new GraphicsPath()) 
     { 
      path.AddString("6. This is a test using GraphicsPath. " + gfx.TextRenderingHint.ToString(), 
          base.Font.FontFamily, (int)base.Font.Style, 
          base.Font.Size, Point.Empty, StringFormat.GenericDefault); 

      gfx.FillPath(Brushes.Black, path); 
     } 
    } 

    e.Graphics.DrawImage(bmp, new Point(10, 110)); 

    bmp.Dispose(); 
    bmp = new Bitmap(width, 20); 

    using (Graphics gfx = Graphics.FromImage(bmp)) 
    { 
     gfx.PageUnit = GraphicsUnit.Pixel; 
     gfx.SmoothingMode = SmoothingMode.HighQuality; 
     gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
     using (GraphicsPath path = new GraphicsPath()) 
     { 
      path.AddString("7. This is a test using GraphicsPath. " + gfx.TextRenderingHint.ToString(), 
          base.Font.FontFamily, (int)base.Font.Style, 
          base.Font.Size, Point.Empty, StringFormat.GenericDefault); 

      gfx.FillPath(Brushes.Black, path); 
     } 
    } 

    e.Graphics.DrawImage(bmp, new Point(10, 130)); 

} 

Form1_v2

+0

您是否嘗試過'TextRenderingHint.ClearTypeGridFit'?編輯:如果你做了「測試4」版本會發生什麼,但是使用了「TextRenderingHint.SystemDefault」呢? –

+1

+1好問題我之前也遇到過這個問題,但沒有提出問題 –

+0

@ChrisSinclair ClearTypeGridFit也不會看起來像它。 SystemDefault是圖形對象初始化時的默認值,它看起來像#2。 – test

回答

2

。在你的代碼中的錯誤,您忘記初始化位圖。它將填充Color.Transparent,黑色,alpha爲0的像素。繪製文本時,Graphics.DrawString()將實現您要求的TextRenderingHint,並對文本進行消除鋸齒。但是文字的前景色是黑色的。背景顏色是黑色的。所以抗鋸齒像素從黑色混合到黑色。徹底破壞抗鋸齒效果,它將字母形狀變成一個斑點。修復:

Bitmap bmp = new Bitmap(width, 20); 
using (Graphics gfx = Graphics.FromImage(bmp)) 
{ 
    gfx.Clear(this.BackColor); 
    // etc... 
} 
+0

確實是這個問題,謝謝你,Hans。 – test

相關問題