使用方法获取嵌入字体会导致受保护的内存错误

ρσݥ*_*zση 0 c# memory fonts protected

我使用此代码来获取嵌入字体:

/// <summary>
    /// Returns an Embedded Font
    /// </summary>
    /// <param name="ImagePath">String begins with namespace e.g MyProgram.Image.png</param>
    /// <returns></returns>
    public static Font GetEmbeddedFont(string FontPath, float Size)
    {
        Font _font = null;
        Thread getFontThread = new Thread(() => GetFont(FontPath, Size, out _font));
        getFontThread.Start();
        getFontThread.Join();
        return _font;            
    }
    #region GetFont
    private static void GetFont(string FontPath, float Size, out Font FontOut)
    {
        Font fnt = null;
        Assembly asm = Assembly.GetExecutingAssembly();
        Stream resStream = asm.GetManifestResourceStream(FontPath);
        if (null != resStream)
        {
            // 
            // GDI+ wants a pointer to memory, GDI wants the memory.
            // We will make them both happy.
            //
            // First read the font into a buffer
            byte[] rgbyt = new Byte[resStream.Length];
            resStream.Read(rgbyt, 0, rgbyt.Length);
            resStream.Close();
            // Then do the unmanaged font (Windows 2000 and later)
            // The reason this works is that GDI+ will create a font object for
            // controls like the RichTextBox and this call will make sure that GDI
            // recognizes the font name, later.
            uint cFonts;
            AddFontMemResourceEx(rgbyt, rgbyt.Length, IntPtr.Zero, out cFonts);
            // Now do the managed font
            IntPtr pbyt = Marshal.AllocCoTaskMem(rgbyt.Length);
            if (null != pbyt)
            {
                Marshal.Copy(rgbyt, 0, pbyt, rgbyt.Length);
                m_pfc = new PrivateFontCollection();
                m_pfc.AddMemoryFont(pbyt, rgbyt.Length);
                Marshal.FreeCoTaskMem(pbyt);
            }
        }

        if (m_pfc.Families.Length > 0)
        {
            // Handy how one of the Font constructors takes a
            // FontFamily object, huh? :-)
            fnt = new Font(m_pfc.Families[0], Size);
        }
        m_pfc.Dispose();    
        FontOut = fnt;            
    }
Run Code Online (Sandbox Code Playgroud)

我使用一个Thread来尝试等待它完成,因为如果在短时间内多次调用此方法,则似乎会发生错误.

我怎么能阻止这个错误发生,我认为这与在彼此快速连续中被调用的方法有关.

我在这里得到例外:

_font = value;
            using (Graphics g = _parent.CreateGraphics())
            {
                SizeF soize = g.MeasureString(_text, _font);
                _size = new Size((int)soize.Width, (int)soize.Height);
                _width = _size.Width;
                _height = _size.Height;
            }
Run Code Online (Sandbox Code Playgroud)

在线g.MeasureString(_text,_font);

但是我知道错误在GetEmbeddedFont方法中,因为如果使用GetEmbeddedFont方法设置字体,它只会引发错误.

它会工作一次,但如果它在第一次之后不久第二次使用,它将抛出错误.

如果我调试代码,_font会返回:

{Name = '((System.Drawing.Font)(_font)).fontFamily.Name' threw an exception of type 'System.ArgumentException' Size=15.0}
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 5

AddMemoryFont()存在文档问题,它没有指定指针需要保持多长时间.我一直选择保守路线,并确保不释放内存,直到使用专用字体的程序停止.这运作良好,没有违规行为.

因此,我强烈建议您删除Marshal.FreeCoTaskMem()调用.完全不同,Windows会在程序终止时自动清理,或者将其移动到FormClosed事件.您的m_pfc.Dispose()调用也是如此.在完成资源之前不要释放资源.