How to delete the file of a PrivateFontCollection.AddFontFile?

前端 未结 1 1799
感情败类
感情败类 2020-12-10 07:07

We create a large count of fonts for a short use. The fonts are embedded in documents. I want delete the font files if not use anymore. How can we do this? The follow simpli

相关标签:
1条回答
  • 2020-12-10 07:23

    After looking in the code of method AddFontFile:

    public void AddFontFile(string filename)
    {
        IntSecurity.DemandReadFileIO(filename);
        int num = SafeNativeMethods.Gdip.GdipPrivateAddFontFile(new HandleRef(this, this.nativeFontCollection), filename);
        if (num != 0)
        {
            throw SafeNativeMethods.Gdip.StatusException(num);
        }
        SafeNativeMethods.AddFontFile(filename);
    }
    

    we see that the font is registered 2 times. First in GDI+ and in the last line in GDI32. This is different to the method AddMemoryFont. In the Dispose method it is only unregistered in GDI+. This result in a leak in GDI32.

    To compensate this you can call the follow:

    [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int RemoveFontResourceEx(string lpszFilename, int fl, IntPtr pdv);
    
    pfc.AddFontFile(fontFile);
    RemoveFontResourceEx(fontFile, 16, IntPtr.Zero);
    
    0 讨论(0)
提交回复
热议问题