来自Resources的addFontFile

我使用以下代码添加了自定义字体:

PrivateFontCollection pfc = new PrivateFontCollection(); pfc.AddFontFile("C:\\Path To\\YourFont.ttf"); label1.Font = new System.Drawing.Font(pfc.Families[0], 16, FontStyle.Regular); 

我在资源中添加了字体文件。 如何从资源添加addFontFile

如果您在资源中包含您的字体

试试这个function

 private void addfontfrommemory() { Stream fontStream = this.GetType().Assembly.GetManifestResourceStream("yourfont.ttf"); byte[] fontdata = new byte[fontStream.Length]; fontStream.Read(fontdata,0,(int)fontStream.Length); fontStream.Close(); unsafe { fixed(byte * pFontData = fontdata) { pfc.AddMemoryFont((System.IntPtr)pFontData,fontdata.Length); } } } 

编辑

如何从汇编中加载资源:(YourNamespace.file.ttf)

 Stream fontStream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("WindowsFormsApplication1.SBADR.TTF"); 

我的解决方案探索者:

在此处输入图像描述

 private static void AddFontFromResource(PrivateFontCollection privateFontCollection, string fontResourceName) { var fontBytes = GetFontResourceBytes(typeof (App).Assembly, fontResourceName); var fontData = Marshal.AllocCoTaskMem(fontBytes.Length); Marshal.Copy(fontBytes, 0, fontData, fontBytes.Length); privateFontCollection.AddMemoryFont(fontData, fontBytes.Length); // Marshal.FreeCoTaskMem(fontData); Nasty bug alert, read the comment } private static byte[] GetFontResourceBytes(Assembly assembly, string fontResourceName) { var resourceStream = assembly.GetManifestResourceStream(fontResourceName); if (resourceStream == null) throw new Exception(string.Format("Unable to find font '{0}' in embedded resources.", fontResourceName)); var fontBytes = new byte[resourceStream.Length]; resourceStream.Read(fontBytes, 0, (int)resourceStream.Length); resourceStream.Close(); return fontBytes; } 

这就是我这样做的方式。

首先获取Font.ttf文件并使用Visual Studio,将文件拖放到根文件夹或资源文件夹中。

在解决方案资源管理器中,右键单击该文件并单击属性。 选择Build Action = Content 。 这将在项目属性>发布>应用程序文件下的应用程序文件中显示该文件。 您将看到现在可以选择文件(默认情况下会自动包含该文件)。

ClickOnce现在将文件复制到StartupPath

要使用它,请遵循以下示例:

 PrivateFontCollection pfc = new PrivateFontCollection(); pfc.AddFontFile(Path.Combine(Application.StartupPath, "font_name.ttf")); textBox1.Font = new Font(pfc.Families[0], 18, FontStyle.Regular); 

这会将字体加载到私有字体集合中,并避免使用上面的示例看到的任何对象引用和内存运行时错误。

出于性能原因,我们只想加载一次字体,并在调用之间保持对字体的引用以进行多次绘图操作。 诀窍是确保如果保留对已创建的Font对象的引用,则PrivateFontCollection不会超出范围。

添加一些静态(共享)变量

 Private Shared _sharedFont As Font Private Shared _sharedFontCollection As Text.PrivateFontCollection Private Shared _sharedFontSize As Integer 

然后声明这些function

 Private Function LoadSharedFont(ByVal fontName As String, ByVal size As Integer, ByVal style As FontStyle) As Font 'Check if font name or size has changed, then clear cache If size <> _sharedFontSize Then _sharedFont = Nothing If _sharedFont Is Nothing Then 'Make this shared so this variable doesnt go out of scope and is garbage collected _sharedFontCollection = New Text.PrivateFontCollection() _sharedFont = LoadFont(fontName, size, style) _sharedFontSize = size End If Return _sharedFont End Function 

 Private Function LoadFont(ByVal fontName As String, ByVal size As Integer, ByVal style As FontStyle) As Font Dim executingAssembly As System.Reflection.Assembly = Reflection.Assembly.GetCallingAssembly() Dim myNamespace As String = executingAssembly.GetName().Name.ToString() Try Using fontstream = executingAssembly.GetManifestResourceStream(myNamespace + "." + fontName) Dim fontBytes(CInt(fontstream.Length)) As Byte fontstream.Read(fontBytes, 0, CInt(fontstream.Length)) Dim fontData As System.IntPtr = Marshal.AllocCoTaskMem(fontBytes.Length) Marshal.Copy(fontBytes, 0, fontData, fontBytes.Length) _sharedFontCollection.AddMemoryFont(fontData, fontBytes.Length) Marshal.FreeCoTaskMem(fontData) End Using Return New Font(_sharedFontCollection.Families(0), size, style) Catch ex As Exception 'An unexpected error has occurred so return a default Font just in case. Return New Drawing.Font("Arial", size, FontStyle.Regular) End Try End Function 

使用方法如下:

 Dim font = LoadSharedFont("OpenSans-CondBold.ttf", 12, FontStyle.Bold)