将 ttf 嵌入为嵌入资源:无法引用它 [英] embed ttf as embeded resource: can't reference it
问题描述
我刚刚将一个 ttf 文件添加到项目 (c# 2008 express) 作为文件"和嵌入资源的构建选项.
我在尝试设置这种字体时遇到问题:(我知道下一行是错的...)
this.label1.Font = AlarmWatch.Properties.Resources.Baby_Universe;
<块引用>
错误 1 不能隐式转换类型'字节[]'到'System.Drawing.Font' C:\Users\hongo\Documents\Visual工作室2008\Projects\AlarmWatch\AlarmWatch\Form1.Designer.cs 57 32 AlarmWatch
我知道它是 byte[] 因为我已将选项 build 设置为嵌入式资源,但与正确的这一行进行比较:
this.label1.Font = new System.Drawing.Font("OCR A Extended",24F,System.Drawing.FontStyle.Regular,System.Drawing.GraphicsUnit.Point, ((byte)(0)));
如何设置 this.label1 以使用新字体?
System.Drawing.Text
命名空间中有一个 AddMemoryFont
方法,它从一个内存(它需要一个指向内存块的指针,所以你需要做一些不安全的操作来获取指向你的字节数组的指针 - 我找到了一个 示例).详细了解 MSDN 上的方法.
还有一个相关的 StackOverflow 问题展示了如何导入Win API函数直接加载字体(以防上述.NET方法不起作用).
编辑 Visual Basic 中关键部分的翻译可能如下所示(虽然尚未检查):
//这应该是某个类的字段PrivateFontCollection pfc = new PrivateFontCollection();//分配内存并将 byte[] 复制到该位置IntPtr 数据 = Marshal.AllocCoTaskMem(yourByteArray.Length);Marshal.Copy(yourFontArray, 0, data, yourFontArray.Length);//将字体传递给字体集合pfc.AddMemoryFont(data, fontStream.Length)//释放不安全的内存Marshal.FreeCoTaskMem(数据)
一旦你有了这个,你应该能够使用它的常用名称来引用字体.
I've just added a ttf file to the project (c# 2008 express) as "file" and build option to embedded resource.
I'm having problems when trying to set this font like this: (I know the next line is wrong...)
this.label1.Font = AlarmWatch.Properties.Resources.Baby_Universe;
Error 1 Cannot implicitly convert type 'byte[]' to 'System.Drawing.Font' C:\Users\hongo\Documents\Visual Studio 2008\Projects\AlarmWatch\AlarmWatch\Form1.Designer.cs 57 32 AlarmWatch
I know it is byte[] cause I've set the option build as embedded resource, but comparing with this line that is correct:
this.label1.Font = new System.Drawing.Font("OCR A Extended",
24F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
How can I set this.label1 to use the new font?
There is a AddMemoryFont
method in the System.Drawing.Text
namespace, which loads a font from a memory (it takes a pointer to the memory block, so you'll need to do some unsafe operation to get pointer to your byte array - I found an example here). More about the method on MSDN.
There is also a related StackOverflow question showing how to import Win API function to load the font directly (in case the above .NET method doesn't work).
EDIT A translation of the key part from Visual Basic might look like this (haven't checked it though):
// This should be probably a field of some class
PrivateFontCollection pfc = new PrivateFontCollection();
// allocate memory and copy byte[] to the location
IntPtr data = Marshal.AllocCoTaskMem(yourByteArray.Length);
Marshal.Copy(yourFontArray, 0, data, yourFontArray.Length);
// pass the font to the font collection
pfc.AddMemoryFont(data, fontStream.Length)
// Free the unsafe memory
Marshal.FreeCoTaskMem(data)
Once you have this, you should be able to refer to the font using its usual name.
这篇关于将 ttf 嵌入为嵌入资源:无法引用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!