代码之家  ›  专栏  ›  技术社区  ›  Will Dean

使用WinForms生成非抗锯齿字体

  •  0
  • Will Dean  · 技术社区  · 16 年前

    我已尝试通过以下方式使用WinForms执行此操作:

      using(Font smoothFont = new Font("Arial", 30, GraphicsUnit.Pixel))
      {
        LOGFONT lf = new LOGFONT();
        smoothFontToLogFont(lf);
        lf.lfQuality = NONANTIALIASED_QUALITY;
        using (Font roughFont = Font.FromLogFont(lf))
        {
    

    但粗糙字体似乎仍能呈现清晰的文本。

    我应该放弃使用WinForms,直接在C中完成这项工作,还是我在这里遗漏了什么?(我的LOGFONT类和相关的lfQuality def直接来自框架源代码,所以我很高兴它们是正确的)

    2 回复  |  直到 16 年前
        1
  •  1
  •   Will Dean    16 年前

    原来我找错了地方,你不能用这种方式更改GDI+字体渲染,你需要在图形对象上设置TextRenderingHint属性,如下所示:

    gr.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit
    
        2
  •  0
  •   Bartosz Wójcik    12 年前

    我知道这与位图无关,但我一直在寻找解决问题的方法,并对LabelEx进行了修改

    using System.Drawing;
    using System.Drawing.Text;
    
    class LabelEx : System.Windows.Forms.Label
    {
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
            base.OnPaint(e);
        }
    }
    

    也许有人会发现它很有用