简体   繁体   English

C# - 如何在不将其安装到系统中的情况下使用自定义字体

[英]C# - How to use a custom Font without installing it in the system

Once again I need your help. 我再次需要你的帮助。

I'm developing a small application on C# that uses a custom Font. 我正在使用自定义Font在C#上开发一个小应用程序。 The problem is, the font must be installed previously on the system. 问题是,字体必须先安装在系统上。 If the font is not present in the system it just uses Times New Roman. 如果系统中没有该字体,则只使用Times New Roman。 Is there any way to embed the font file in the application so it doesn't need to be installed in every system? 有没有办法在应用程序中嵌入字体文件,因此不需要在每个系统中安装它?

Thank you. 谢谢。

If you're still reading this, I might point out that you don't have to use unsafe code to load the font from a resource. 如果您还在阅读本文,我可能会指出您不必使用不安全的代码来加载资源中的字体。 Here's an example using Marshal. 这是使用Marshal的一个例子。

PrivateFontCollection _fonts = new PrivateFontCollection();

byte[] fontData = Resources.CustomFontResourceName;

IntPtr fontPtr = Marshal.AllocCoTaskMem(fontData.Length);

Marshal.Copy(fontData, 0, fontPtr, fontData.Length);

_fonts.AddMemoryFont(fontPtr, fontData.Length);

Marshal.FreeCoTaskMem(fontPtr);

Font customFont = new Font(_fonts.Families[0], 6.0F);

Note that if you use AddMemoryFont, you will need to use this api call "AddFontMemResourceEx" or it wont work. 请注意,如果您使用AddMemoryFont,则需要使用此api调用“AddFontMemResourceEx”,否则它将无法工作。

PrivateFontCollection giving me symbols PrivateFontCollection给我符号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM