积极答复者
C#制作winforms程序,如何让程序使用资源文件里面的字体?(VS2010)

问题
-
我做了一个程序,上面用了特殊字体,我不想用户安装这种字体,所以想放到资源文件里面调用出来。
目前还未到发布(还未清楚如何脱离开发环境让用户安装此程序),还在调试阶段
我查了很多资料,大致都说是把字体嵌入到资源文件Resources.resx里面,然后用流的方式读出来(字体属于二进制文件)
但问题是,设置字体的font属性都是font类,而用流方式读出来无法转成合适的font类型(例如form1.font)
请问如何通过资源文件的方式让用户使用特殊字体?
谢谢!
<embed height="0" hidden="true" id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" width="0" />
<embed height="0" hidden="true" id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" width="0" />- 已编辑 啊辉 2012年5月24日 0:50
答案
-
测试效果:
测试代码:
namespace WindowsFormsApplication2 { public partial class Form1 : Form { PrivateFontCollection pfc = new PrivateFontCollection(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string fontName = "WindowsFormsApplication2.汉仪水波体简.ttf"; Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream(fontName); byte[] fontData = new byte[stream.Length]; stream.Read(fontData, 0, (int)stream.Length); stream.Close(); unsafe { fixed (byte* pFontData = fontData) { pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length); } } } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Font font = new Font(pfc.Families[0], 20); g.DrawString("测试文字", font, new SolidBrush(Color.Black), 200, 100); } } }
记得准备一个ttf字体测试文件,添加到WinForm项目中,并设置为Enbedded Resource
- 已建议为答案 ThankfulHeartModerator 2012年5月25日 5:03
- 已标记为答案 啊辉 2012年5月26日 15:28
全部回复
-
-
生成font文件放到windows\fonts目录下
谢谢回答
但是我不想在用户系统里安装任何文件,另外这个程序想运行在WIN7,这个做法不适合
<embed height="0" hidden="true" id="lingoes_plugin_object" type="application/lingoes-npruntime-capture-word-plugin" width="0" /> -
测试效果:
测试代码:
namespace WindowsFormsApplication2 { public partial class Form1 : Form { PrivateFontCollection pfc = new PrivateFontCollection(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string fontName = "WindowsFormsApplication2.汉仪水波体简.ttf"; Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream(fontName); byte[] fontData = new byte[stream.Length]; stream.Read(fontData, 0, (int)stream.Length); stream.Close(); unsafe { fixed (byte* pFontData = fontData) { pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length); } } } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Font font = new Font(pfc.Families[0], 20); g.DrawString("测试文字", font, new SolidBrush(Color.Black), 200, 100); } } }
记得准备一个ttf字体测试文件,添加到WinForm项目中,并设置为Enbedded Resource
- 已建议为答案 ThankfulHeartModerator 2012年5月25日 5:03
- 已标记为答案 啊辉 2012年5月26日 15:28
-
测试效果:
测试代码:
namespace WindowsFormsApplication2 { public partial class Form1 : Form { PrivateFontCollection pfc = new PrivateFontCollection(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string fontName = "WindowsFormsApplication2.汉仪水波体简.ttf"; Assembly assembly = Assembly.GetExecutingAssembly(); Stream stream = assembly.GetManifestResourceStream(fontName); byte[] fontData = new byte[stream.Length]; stream.Read(fontData, 0, (int)stream.Length); stream.Close(); unsafe { fixed (byte* pFontData = fontData) { pfc.AddMemoryFont((System.IntPtr)pFontData, fontData.Length); } } } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Font font = new Font(pfc.Families[0], 20); g.DrawString("测试文字", font, new SolidBrush(Color.Black), 200, 100); } } }
记得准备一个ttf字体测试文件,添加到WinForm项目中,并设置为Enbedded Resource
我有一个疑问,是否一定要用非安全代码才能实现?