2015-06-15 111 views
1

我有以下的C#代碼來獲得在Windows 7從位圖圖形的64位抗鋸齒文本不抗鋸齒和的ClearType文本在窗口非常難看形成

protected Graphics GetImageGraphics() 
{ 
    var g = Graphics.FromImage(image); 
    g.SmoothingMode = SmoothingMode.HighQuality; 
    g.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; 
    g.FillRectangle(backBrush, new Rectangle(Point.Empty, Bounds.Size)); 
    return g; 
} 

這是用來如下:

g.DrawImageUnscaled(image, Bounds); 

所得到的文本爲v:

protected override void Redraw() 
{ 
    var g = GetImageGraphics(); 

    var path = RoundedRectanglePath(shapeRect, CornerRadius); 
    g.FillPath(selected ? selectedBrush : brush, path); 
    g.DrawPath(pen, path); 

    foreach(var pin in pins) 
     pin.Value.Render(g); 

    var labelMax = shapeRect; 
    labelMax.Inflate(-10, -10); 

    var fmt = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak | TextFormatFlags.WordEllipsis; 
    TextRenderer.DrawText(g, Task.Symbol.Label, Font, labelMax, Color.Black, fmt); 

    fmt = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis; 
    TextRenderer.DrawText(g, Task.TaskId, Font, idRect, Color.Black, fmt); 
} 

創建的圖像,然後使用拷貝到屏幕醜陋。如果我將TextRenderingHint更改爲AntiAlias或AntiAliasGridFit,則字體呈現得更清晰,但沒有顯示任何抗鋸齒(如SingleBitPerPixel)

我在做什麼錯?

+0

您必須繪製背景以進行消除鋸齒工作。例如,添加g.Clear(Color.White)。 –

回答

0

該解決方案是雙重的。正如漢斯指出的那樣,文字不會變成透明的。答案的另一部分是不在內存位圖上使用TextRenderer,如此處所述https://stackoverflow.com/a/1578056/429328