[英]How can I bundle a font with my .net winforms application?
I'd like to use a non-standard font for my .net 3.0 Winforms application. 我想为.net 3.0 Winforms应用程序使用非标准字体。
This font might be installed on some of my user's computer, but it will clearly be missing on some others. 该字体可能已安装在我的用户的某些计算机上,但在其他一些计算机上显然会丢失。
How can I ship the font with my program ? 如何在程序中附带字体? Do I need to install the font ?
我需要安装字体吗? If so, is the lack of administrator privileges going to be an issue?
如果是这样,缺少管理员特权会成为问题吗?
You'll have to use an installer to get the font registered on the target machine. 您必须使用安装程序来在目标计算机上注册字体。 But maybe you won't have to, GDI+ supports private fonts .
但也许您不必,GDI +支持专用字体 。
Here's a blog article I wrote that shows a way to embed fonts as resources in your application (no dll imports necessary :). 这是我写的一篇博客文章,展示了一种将字体作为资源嵌入应用程序中的方法(无需dll导入:)。
Embedding Fonts in your .Net Application 在.Net应用程序中嵌入字体
Here's the class I create where all the magic happens. 这是我创建的所有发生魔术的课程。 The blog article includes instructions and an example of using it.
该博客文章包括说明和使用示例。
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.InteropServices;
namespace EmbeddedFontsExample.Fonts
{
public class ResFonts
{
private static PrivateFontCollection sFonts;
static ResFonts()
{
sFonts = new PrivateFontCollection();
// The order the fonts are added to the collection
// should be the same as the order they are added
// to the ResFontFamily enum.
AddFont(MyFonts.Consolas);
}
private static void AddFont(byte[] font)
{
var buffer = Marshal.AllocCoTaskMem(font.Length);
Marshal.Copy(font, 0, buffer, font.Length);
sFonts.AddMemoryFont(buffer, font.Length);
}
public static Font Create(
ResFontFamily family,
float emSize,
FontStyle style = FontStyle.Regular,
GraphicsUnit unit = GraphicsUnit.Pixel)
{
var fam = sFonts.Families[(int)family];
return new Font(fam, emSize, style, unit);
}
}
public enum ResFontFamily
{
/// <summary>Consolas</summary>
Consolas = 0
}
}
本页详细说明了如何将字体嵌入到winforms项目中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.