加载外部字体并在 C# 中使用它
我想从外部服务器加载字体,一旦加载(我想这是必要的),就用它来创建一些文本字段。
我正在尝试:
font_uri = new Uri("http://localhost/assets/fonts/wingding.ttf");
bf_helvetica = new FontFamily(font_uri, "bf_helvetica");
TextBlock test_tb = new TextBlock();
test_tb.Text = "This is a test";
test_tb.FontSize = 16;
test_tb.Foreground = Brushes.Red;
test_tb.FontFamily = bf_helvetica;
stage.Children.Add(test_tb);
但它使用默认字体创建文本块。 有任何想法吗?
提前致谢 :)
I'd like to load a font from an external server and once is loaded (I guess that's necessary) use it to create a few textfields.
I'm trying:
font_uri = new Uri("http://localhost/assets/fonts/wingding.ttf");
bf_helvetica = new FontFamily(font_uri, "bf_helvetica");
TextBlock test_tb = new TextBlock();
test_tb.Text = "This is a test";
test_tb.FontSize = 16;
test_tb.Foreground = Brushes.Red;
test_tb.FontFamily = bf_helvetica;
stage.Children.Add(test_tb);
But it creates the textblock with the default font.
Any ideas?
Thanks in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以将其加载到流中,请尝试使用 PrivateFontCollection。 我对另一个问题的回答。
编辑:请参阅系统。 Net.WebRequest.GetRequestStream,将 URI 加载到 Stream 中,然后将该 Stream 加载到 PFC 中,如链接代码中所述。
另外,我会将文件保存在本地,然后先在那里查找它,这样您就不必每次运行程序时都下载它。
再次编辑:抱歉,不是 WebRequest.GetRequestStream,您需要 WebResponse.GetResponseStream()。 这里有一些示例代码可以完全满足您的需求。
If you can load it into a Stream, try using a PrivateFontCollection. Example code in my answer to another question.
EDIT: See System.Net.WebRequest.GetRequestStream, load the URI into a Stream, then load that Stream into the PFC as mentioned in the linked code.
Also, I'd save the file locally, and look for it there first, so you don't have to download it every time you run the program.
EDIT AGAIN: Sorry, not WebRequest.GetRequestStream, you want WebResponse.GetResponseStream(). Here's some sample code to do exactly what you're looking for.
您传递给 FontFamily 构造函数的系列名称实际上是字体文件公开的系列名称吗? 在您的示例中,如果您加载了 Wingding.ttf,则字体系列名称将为 Wingdings,而不是 bf_helvetica。 如果字体文件是 bf_helvetica.ttf,则系列名称可能不是字体名称,例如 Helvetica 或 Helvetica Bold。
Is the family name you pass to the FontFamily constructor actually a family name exposed by the font file? In your example, if you loaded the Wingding.ttf, the font family name would be Wingdings, not bf_helvetica. If the font file is bf_helvetica.ttf, the family name is likely something other than the name of the font, like Helvetica or Helvetica Bold.
我发布了一个针对 true type 字体的解决方案,但它可以与其他类型一起使用。
C# 如何添加TTF TO Visual Studio 中的项目
http://hongouru.blogspot.com/2010/10/c-how-to-add-fonts-ttf-true-type-fonts.html
我希望它有帮助。
I posted a solution for true type fonts but it could work with other types.
C# HOW TO ADD A TTF TO the project in Visual Studio
http://hongouru.blogspot.com/2010/10/c-how-to-add-fonts-ttf-true-type-fonts.html
I hope it helps.