Ошибка при попытке загрузить файл шрифта из ресурсов приложения формы VS - PullRequest
0 голосов
/ 17 мая 2019

Я сталкиваюсь с «IndexOutOfRangeException» в строке ff = pfc.Families[0]; при попытке загрузить мой файл шрифта .tff из ресурсов приложения формы VS. Вы думаете, что могли бы посмотреть на мой код и сказать мне, почему и как это исправить?

public partial class MainForm : Form
{
        [DllImport("gdi32.dll")]
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbfont, uint cbfont, IntPtr pdv, [In] ref uint pcFonts);

        FontFamily ff;
        Font font;

        bool NameFieldPopulated = false;
        bool TitleFieldPopulated = false;
        bool LocationFieldPopulated = false;
        bool ExtFieldPopulated = false;
        bool OutputFieldPopulated = false;
        public static string NameInput;
        public static string DirName;
        public static string TitleInput;
        public static string LocationInput;
        public static string PhoneNumber;
        public static string ExtensionInput;
        public static string DirInput;
        public Form2 overwrite;
        public SucessForm success;
        public OverwriteSucessForm succ2;
        public MainForm main;


        public MainForm()
        {
            InitializeComponent();
            GenerateButton.Enabled = false;
        }

        private void loadFont()
        {
            byte[] fontArray = SF_Signature_Generator.Properties.Resources.bignoodletitling;
            int dataLength = SF_Signature_Generator.Properties.Resources.bignoodletitling.Length;

            IntPtr ptrData = Marshal.AllocCoTaskMem(dataLength);

            Marshal.Copy(fontArray, 0, ptrData, dataLength);

            uint cFonts = 0;

            AddFontMemResourceEx(ptrData, (uint)fontArray.Length, IntPtr.Zero, ref cFonts);

            PrivateFontCollection pfc = new PrivateFontCollection();

            pfc.AddMemoryFont(ptrData, dataLength);

            Marshal.FreeCoTaskMem(ptrData);

            ff = pfc.Families[0];
            font = new Font(ff, 15f, FontStyle.Regular);

        }

        private void AllocFont(Font f, Control c, float size)
        {
            FontStyle fontStyle = FontStyle.Regular;

            c.Font = new Font(ff, size, fontStyle);
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            loadFont();
            AllocFont(font, this.NameLabel, 20);
        }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...