C#winform窗体工程中引用资源中的字体时CS1729报错

窗口要用一些奇奇怪怪的字体,我把一个自己下的字体嵌入资源,最开始用缓存的方式在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));

就报错了,来回折腾好几天了,几大网站的文章差不多都看了两遍以上,可这是为什么,我又该怎么办?

PrivateFontCollectionpfc = new PrivateFontCollection();
pfc.AddFontFile(path);
就行了
你已经把文件放到了资源里,那么直接
pfc.AddFontFile(WindowsFormsApplication1.Resources.颖颖情书体_ttf);不就行了
先放字符串里再反射不够你折腾的

var frm = new PrivateFontCollection();
            frm.AddFontFile("颖颖情书体.ttf");
this.label1.Font = new Font(frm.Families[0], 20.25F);

你操作不了资源文件,何苦为难自己呢?

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632