C# - 如何使用自定义字体没有在系统中安装它 [英] C# - How to use a custom Font without installing it in the system

查看:710
本文介绍了C# - 如何使用自定义字体没有在系统中安装它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再一次,我需要你的帮助。

Once again I need your help.

我正在开发一个使用自定义字体在C#中的小应用。
中的问题是,字体必须预先安装在系统上。如果字体不存在系统中它只是使用Times New Roman字体。
有什么办法嵌入字体文件中的应用程序,它并不需要安装在每一个系统?

I'm developing a small application on C# that uses a custom Font. 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. Is there any way to embed the font file in the application so it doesn't need to be installed in every system?

感谢您。

推荐答案

如果你还在读这篇文章,我可能会指出,你不必使用不安全的代码,以从加载字体资源。 。下面是使用元帅为例

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.

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);

这篇关于C# - 如何使用自定义字体没有在系统中安装它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆