C# 如何在WPF中的文本上创建多个笔划?

C# 如何在WPF中的文本上创建多个笔划?,c#,wpf,C#,Wpf,我正在尝试在WPF中创建如下所示的文本: 请注意,它是黄色文本,带有黑色笔划,然后是黄色笔划,然后是另一个(非常薄的)黑色笔划。现在,我可以通过以下方法创建一个简单的笔划,难度很小。请注意,未显示的属性是包含控件的所有DP protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { // Draw the outline based on the properties th

我正在尝试在WPF中创建如下所示的文本:

请注意,它是黄色文本,带有黑色笔划,然后是黄色笔划,然后是另一个(非常薄的)黑色笔划。现在,我可以通过以下方法创建一个简单的笔划,难度很小。请注意,未显示的属性是包含控件的所有DP

protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
    // Draw the outline based on the properties that are set.
    drawingContext.DrawGeometry(Fill, 
                                new System.Windows.Media.Pen(Stroke, StrokeThickness), 
                                _textGeometry);
}

/// <summary> 
/// Create the outline geometry based on the formatted text. 
/// </summary> 
public void CreateText()
{
   System.Windows.FontStyle fontStyle = FontStyles.Normal;
   FontWeight fontWeight = FontWeights.Medium;

   if (Bold == true) fontWeight = FontWeights.Bold;
   if (Italic == true) fontStyle = FontStyles.Italic;

   // Create the formatted text based on the properties set.
   FormattedText formattedText = new FormattedText(
      Text,
      CultureInfo.GetCultureInfo("en-us"),
      FlowDirection.LeftToRight,
      new Typeface(
          Font,
          fontStyle,
          fontWeight,
          FontStretches.Normal),
      FontSize,
      System.Windows.Media.Brushes.Black
    );

    // Build the geometry object that represents the text.
     _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0));
}
protected override void OnRender(System.Windows.Media.DrawingContext DrawingContext)
{
//根据设置的特性绘制轮廓。
drawingContext.DrawGeometry(填充、,
新系统.Windows.Media.Pen(笔划、笔划厚度),
_文本几何);
}
///  
///基于格式化文本创建轮廓几何图形。
///  
public void CreateText()
{
System.Windows.FontStyle FontStyle=FontStyles.Normal;
FontWeight FontWeight=FontWeights.中等;
如果(粗体==true)fontWeight=FontWeights.Bold;
如果(Italic==true)fontStyle=FontStyles.Italic;
//基于属性集创建格式化文本。
FormattedText FormattedText=新的FormattedText(
文本,
文化信息。获取文化信息(“美国”),
FlowDirection.LeftToRight,
新字体(
字体,
方特风格,
方特威克,
(正常),,
字体大小,
System.Windows.Media.Brusks.Black
);
//生成表示文本的几何体对象。
_textGeometry=formattedText.BuildGeometry(新的System.Windows.Point(0,0));
}

所以我的问题是,我该如何处理这个问题,并在其中添加另一个(或几个其他)笔划?

一种方法是将笔划几何体与初始几何体相结合,然后为第二个笔划添加笔划几何体

幸运的是,.NET证明了它可以获得笔划几何体。然后,您可以使用将两者结合起来:

_combinedGeometry = Geometry.Combine(_textGeometry, 
        _textGeometry.GetWidenedPathGeometry(new Pen(Stroke, StrokeThickness * 2)), 
        GeometryCombineMode.Union, null);
注意
StrokeThickness*2
。您需要这样做是因为WPF绘制了一个中心笔划,如果没有它,第二个笔划将至少部分(如果不是完全)覆盖第一个笔划。然后像以前一样绘制,填充为
null

drawingContext.DrawGeometry(null, 
        new System.Windows.Media.Pen(SecondaryStroke, SecondaryStrokeThickness), 
        _combinedGeometry);
可以对其他笔划重复此操作,也可以使用带有循环的集合

警告
GetWideedPathGeometry
不会根据您使用的字体、字体大小和笔划大小始终返回“完美”笔划。为了不产生任何瑕疵,您可能需要对其进行一些处理


上述解决方法:如果可能,增加字体大小。它增加了笔划片段之间的距离,降低了算法将“桥接”两个片段或创建其他瑕疵的可能性。

如何在彼此之间添加多个笔划?每一次都是用更小的厚度?@codemonkey可以工作。。。您建议使用相同填充但大小不同的笔划调用多个
DrawGeometry
?我更希望笔划超出原文,但这可能是实现效果的简单方法。我不明白你为什么不使用codemonkey的解决方案。你所说的“我希望笔划超出原文”是什么意思?它不是被渲染成一半在里面,一半在外面吗?如果是这样,那么获得最外层轮廓所需的全部操作就是使用笔划宽度,笔划宽度为从文本几何体到最外层轮廓外部距离的两倍。然后你用类似的数学方法生成所有其他笔划,最后在没有笔划的情况下填充内部。@adv12是的,我以为我的方法是外部笔划,但今天发现它实际上是中心笔划。他的方法会很好(如果不比我的其他方法好的话)。如果这是一个答案,我肯定会投票,可能会接受。为什么不使用LinearGradientBrush呢?