我想在我的WinForms应用程序中嵌入字体,这样我就不必担心它们会被安装到机器上。我在MSDN网站上搜索了一下,发现了一些关于使用本机Windows API调用的提示,例如Michael Caplan的(sp?)由Scott Hanselman链接的教程。现在,我真的要经历这么多麻烦吗?我不能只使用我的应用程序的资源部分吗?
如果不是,我可能会选择安装路线。在这种情况下,我可以通过编程来实现吗?只需将字体文件复制到Windows\Fonts文件夹?
我知道许可问题。
发布于 2014-05-07 21:40:59
这就是我在VS2013中的工作,而不需要使用不安全的代码块。
嵌入资源
将字体加载到内存中
using System.Drawing.Text;
添加到Form1.cs文件中使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用System.Data;使用System.Linq;使用System.Text;使用System.Threading.Tasks;使用System.Windows.Forms;使用System.Reflection;使用System.Drawing.Text;命名空间WindowsFormsApplication1 { public partial Form1 : Form {public private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont,uint cbFont,IntPtr pdv,ref uint );私有虚拟字体= System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);System.Runtime.InteropServices.Marshal.Copy(fontData,();PrivateFontCollection PrivateFontCollection();myFont Form1() { InitializeComponent();byte[] fontData = Properties.Resources.MyFontName;虚拟字体= IntPtr fontPtr 0,fontPtr,fontData.Length);uint dummy = 0;fonts.AddMemoryFont(fontPtr,Properties.Resources.MyFontName.Length);AddFontMemResourceEx(fontPtr,(uint)Properties.Resources.MyFontName.Length,IntPtr.Zero,ref dummy);fonts.AddMemoryFont myFont =新字体(fonts.Families,16.0F);}
使用您的字体
private void Form1_Load(对象发送者,EventArgs e) { label1.Font = myFont;}
发布于 2011-05-26 17:29:40
// specify embedded resource name
string resource = "embedded_font.PAGAP___.TTF";
// receive resource stream
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
// create an unsafe memory block for the font data
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
// create a buffer to read in to
byte[] fontdata = new byte[fontStream.Length];
// read the font data from the resource
fontStream.Read(fontdata, 0, (int)fontStream.Length);
// copy the bytes to the unsafe memory block
Marshal.Copy(fontdata, 0, data, (int)fontStream.Length);
// pass the font to the font collection
private_fonts.AddMemoryFont(data, (int)fontStream.Length);
// close the resource stream
fontStream.Close();
// free up the unsafe memory
Marshal.FreeCoTaskMem(data);
发布于 2014-07-21 20:12:12
我将一个randomly downloaded font拖放到我的资源中,这起作用了。但是使用了一个文件。我猜你也可以用它来安装字体吧?
public Form1()
{
string filename = @"C:\lady.gaga";
File.WriteAllBytes(filename, Resources.KBREINDEERGAMES);
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(filename);
Label label = new Label();
label.AutoSize = true;
label.Font = new Font(pfc.Families[0], 16);
label.Text = "hello world";
Controls.Add(label);
}
https://stackoverflow.com/questions/556147
复制相似问题