【问题标题】:Generating non-anti-aliased fonts with WinForms使用 WinForms 生成非抗锯齿字体
【发布时间】:2009-04-08 22:24:02
【问题描述】:

我需要生成一个位图,其中包含一些未使用抗锯齿或 ClearType 渲染的字符。

在 Win32 领域,我会创建一个将 lfQuality 设置为 NONANTIALIASED_QUALITY 的字体并用它来绘制。

我已尝试通过以下方式使用 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))
    {

但 roughFont 似乎仍然呈现 ClearTyped 文本。

我应该放弃 WinForms 并在 C 中执行此操作,还是我在这里遗漏了什么? (我的 LOGFONT 类和相关的 lfQuality defs 直接来自框架源,所以我很高兴它们是正确的)

【问题讨论】:

    标签: winforms fonts


    【解决方案1】:

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

    gr.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit
    

    【讨论】:

    • 谢谢!这个解决方案为我省去了很多麻烦。我以前的超级模糊字体现在看起来很棒:)
    【解决方案2】:

    我知道这与位图无关,但我一直在为我的问题寻找解决方案并提出了这个 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);
        }
    }
    

    也许有人会觉得它有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-12
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多