How to always get English font name via PrivateFontCollection in C#?

喜你入骨 提交于 2019-12-12 03:03:55

问题


Now I am getting the font name by the following code:

PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile("path_to_my_Thai_font");
return fontCollection.Families[0].Name;

On a English Win7 system, I get "DFPHeiMedium-UN", but on a Chinese Win7 system, I get "華康中黑體(P)-UN". So, the font name differs in different system languages.

My question is how to write my code to make sure that I can always get "DFPHeiMedium-UN" regardless of the system language?

Thank you.

By the way, I tried CurrentCulture. I put this line before the above code. Still don't work. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-us");


回答1:


You can use the GetName function which accepts a language identifier, or 0 for the neutral language:

PrivateFontCollection fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile("path_to_my_Thai_font");
return fontCollection.Families[0].GetName(0);

As to why you culture change didn't work, we can look at the implementation of Name in the reference source:

    public String Name 
    {
        get 
        {   
            return GetName(CurrentLanguage);
        }
    }

And if we inspect CurrentLanguage, we can see that it's using System.Globalization.CultureInfo.CurrentUICulture.LCID, that is, the current UI culture, rather than CurrentCulture.



来源:https://stackoverflow.com/questions/41053962/how-to-always-get-english-font-name-via-privatefontcollection-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!