【问题标题】:How to use old MS Sans Serif Font如何使用旧的 MS 无衬线字体
【发布时间】:2016-04-24 14:05:53
【问题描述】:

我正在开发一个在位图上绘制文本的程序。我必须使用旧的MS Sans Serif 72 字体,因为我需要一个大像素化字体

我在C:\Windows\Fonts 文件夹中找到了这种字体,但是当我使用这样的代码时:

Font myFont("MS Sans Serif", 72F, FontStyle.Regular, GraphicsUnit.Pixel)
myGraphics.DrawString(string1, font, solidBrush, New PointF(100, 10))

然后将myFont 设置为Microsoft Sans Serif,而不是MS Sans Serif。为什么 Windows 将其更改为 TrueType 字体,我如何使用 .fon 文件?

你能告诉我如何使用MS Sans Serif吗?

【问题讨论】:

    标签: c# .net vb.net fonts


    【解决方案1】:

    .NET 仅支持使用 TrueType 字体 (*.ttf),以兼容 GDI+。

    在 .NET 中使用光栅字体 (*.fon) 很困难,并且需要使用 Interop 来访问 GDI 方法。 有关使用 TextOut 可能如何工作的一些示例,请参阅 pinvoke.net

    一个更简单的选择可能是尝试将您的文本渲染为位图,然后放大位图以创建像素化效果,例如:

    int width = 80;
    int height = 80;
    
    using (Bitmap bitmap = new Bitmap(width, height))
    {
        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            var font = new Font("MS Sans Serif", 16, FontStyle.Regular, GraphicsUnit.Point);
            graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
            graphics.DrawString("012345", font, Brushes.Black, 0, 0);
        }
    
        e.Graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
        e.Graphics.DrawImage(bitmap, ClientRectangle, 0, 0, width, height, GraphicsUnit.Pixel);
    }
    

    更新:添加了@HansPassant 评论的改进。

    【讨论】:

    • 添加 graphics.TextRenderingHint 以禁用抗锯齿。并使位图更小,因此必须使用 InterpolationMode.NearestNeighbor 进行放大
    • 谢谢!像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 2011-12-07
    • 2015-03-11
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2015-01-24
    • 2021-12-18
    • 2011-01-10
    相关资源
    最近更新 更多