[英]Use Custom Font in DrawText Wpf C#
I have a custom OTF font in the Resources folder in my project which is not installed on my Windows . 我的项目的Resources文件夹中有一个自定义的OTF字体,该字体未安装在Windows上 。 The Build Action of the font is already set to Resource .
字体的“ 生成操作”已设置为Resource 。
Now I want to use DrawText
method and FormattedText
class to write some text on visual layer. 现在,我想使用
DrawText
方法和FormattedText
类在可视层上编写一些文本。
How can I use this custom font for the FormattedText
. 如何为
FormattedText
使用此自定义字体。 I already know how to do this say for a TextBlock in XAML using the below code. 我已经知道如何使用以下代码针对XAML中的TextBlock执行此操作。 But what about code-behind?
但是代码隐藏呢?
<TextBlock FontFamily="pack://application:,,,/Resources/#Quicksand Light">
StackOverflow
</TextBlock>
Here is the code I'm using to define my FormattedText object. 这是我用来定义FormattedText对象的代码。
var f = new FontFamily("pack://application:,,,/Resources/#Quicksand Light");
var typeface = new Typeface(f, new FontStyle(), new FontWeight(), new FontStretch());
var cultureinfo = new CultureInfo("en-us");
var ft = new FormattedText("Stackoverflow", cultureinfo, FlowDirection.LeftToRight,
typeface, 28, Brushes.White)
dc.DrawText(ft, new Point(0,0));
My problem is is defining the font for typeface so that I can use it in fotmattedtext. 我的问题是为字体定义字体,以便可以在fotmattedtext中使用它。
像这样:
var typeface = new Typeface(new FontFamily(new Uri("pack://application:,,,/"), "/Resources/#Quicksand Light"), FontStyles.Normal, FontWeights.Regular, FontStretches.Normal);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.