Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

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

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");

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...