窗口要用一些奇奇怪怪的字体,我把一个自己下的字体嵌入资源,最开始用缓存的方式在program里乱改一通
using System;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Reflection;
using System.IO;
namespace WindowsFormsApplication1
{
internal static class Program
{
public static PrivateFontCollection privateFontCollection = new PrivateFontCollection();
[STAThread]
private static void Main()
{
string fontName = "WindowsFormsApplication1.Resources.颖颖情书体_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)
{
privateFontCollection.AddMemoryFont((System.IntPtr)pFontData, fontData.Length);
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(defaultValue: false);
Application.Run( new Form1());
}
}
}
然后在form1.designer里输入
this.label1.Font = new System.Drawing.Font(Program.privateFontCollection.Families[0], 20.25F);
它告诉我索引超出了数组界限,而且我搬到别的电脑上试运行是字体变回默认了。于是我把program改了回去,把刚才那个改成
this.label1.Font = new System.Drawing.Font(((byte[])(resources.GetObject("颖颖情书体.ttf")), 20.25F));
就报错了,来回折腾好几天了,几大网站的文章差不多都看了两遍以上,可这是为什么,我又该怎么办?