本文介绍了在 .NET 应用程序中嵌入/部署自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种官方方式可以通过 .NET 应用程序分发(部署)特定字体?

Is there an official way to distribute (deploy) a specific font with a .NET application?

我们有一个(公共领域)LED 字体",可以打印具有复古 LED 仪表外观的数字.这是一种标准的 True Type 或 Open Type 字体,与其他字体一样,只是看起来很时髦.

We have a (public domain) "LED font" that prints numbers with the retro LED instrumentface look. This is a standard True Type or Open Type font like any other except it looks funky.

显然,要使其工作,该字体需要在用户的机器上.但我们不希望强迫用户将我们的特殊字体安装到您的字体文件夹中".我们更愿意直接从 TTF 加载 Font 对象,或者以编程方式安装字体以使其可用.

Obviously for that to work, this font needs to be on the user's machine. But we'd prefer to not force the user to "install our special font into your font folder". We'd prefer to either load a Font object directly from the TTF, or programatically install the font so it's available.

应用程序如何处理此类事情?例如,我注意到 Adob​​e XYZ 在没有用户干预的情况下在系统上安装了各种字体.这就是我们想要做的.

How do applications handle this sort of things? Eg, I notice Adobe XYZ installs various fonts on the system without user intervention. That's what we'd like to do.

好的,理想情况下,我们希望直接安装字体.我们不希望我们漂亮的主题 LED 字体显示在 MS Word 的用户字体下拉列表中.我们更愿意使用这种字体,但将其使用或外观限制在我们的应用程序中.有什么办法吗?

okay, ideally, we'd prefer not to install the font directly. We don't want our nifty themed LED font showing up in the user's font dropdown in MS Word. We'd prefer to use this font, but restrict its use or appearance to our app. Any way to do this?

编辑 2:这是一个 WinForms .NET 2.0 应用程序.

EDIT 2: This is for a WinForms .NET 2.0 app.

谢谢!

推荐答案

我在一个 asp.net 站点上为我的自定义图形库使用自定义字体,但这应该也适用于 winform问题.您只需指定字体文件、字体大小和字体样式,然后返回字体类型.

I use a custom font for my custom graphics-library on an asp.net site, but this should also work on winform without issues. You just specify the font-file, the font-size and the font-style, and the font-type is returned.

public static LoadedFont LoadFont(FileInfo file, int fontSize, FontStyle fontStyle)
{
    var fontCollection = new PrivateFontCollection();
    fontCollection.AddFontFile(file.FullName);
    if (fontCollection.Families.Length < 0)
    {
        throw new InvalidOperationException("No font familiy found when loading font");
    }

    var loadedFont = new LoadedFont();
    loadedFont.FontFamily = fontCollection.Families[0];
    loadedFont.Font = new Font(loadedFont.FontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
    return loadedFont;
}

LoadedFont 是一个简单的结构

LoadedFont is a simple struct

public struct LoadedFont
{
    public Font Font { get; set; }
    public FontFamily FontFamily { get; set; }
}

这是为了防止 FontFamily 被垃圾回收和字体不起作用(asp.net,我不知道在 winform 上是否需要它).

This is needed to prevent the FontFamily to be GC'ed and the font not working (asp.net, i do not know if it is needed on winform).

这篇关于在 .NET 应用程序中嵌入/部署自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:15