gpt4 book ai didi

c# - 如何使用旧的 MS Sans Serif 字体

转载 作者:行者123 更新时间:2023-11-30 21:52:20 26 4
gpt4 key购买 nike

我正在开发一个在位图上绘制文本的程序。我必须使用旧的 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 吗?

最佳答案

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

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

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

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 的评论添加了改进。

关于c# - 如何使用旧的 MS Sans Serif 字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34871048/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com