gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:56:08 25 4
gpt4 key购买 nike

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

sample text

请注意,它是黄色文本,带有黑色笔划,然后是黄色笔划,然后是另一个(非常细的)黑色笔划。现在,我可以按照 How to: Create Outlined Text 创建笔画.请注意,未显示的属性都是包含控件的 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));
}

所以我的问题是,我如何接受它并向其添加另一个(或多个)笔画?

最佳答案

一种方法是将笔划几何形状与初始几何形状组合,然后为第二个笔划划那个

幸运的是,.NET 证明了 Geometry.GetWidenedPathGeometry用于获取笔划几何图形。然后您可以使用 Geometry.Combine将两者结合起来:

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

您可以对其他笔画重复此操作,或使用带循环的集合。

警告:GetWidenedPathGeometry 不会总是根据您使用的字体、字体大小和笔画大小返回“完美”笔画。为了没有任何人工制品,您可能需要稍微尝试一下。

上述解决方法:如果可能,增大字体大小。它增加了笔画片段之间的距离,降低了算法“桥接”两个片段或产生其他伪像的可能性。

关于c# - 如何在 WPF 中的文本上创建多个笔划?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27625925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com