WPF中,将普通文字转成路径(Path)的方法


public string GetTextPath(string word, string fontFamily, int fontSize)
{
            Typeface typeface = new Typeface(new FontFamily(fontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
            return GetTextPath(word, typeface, fontSize);
}

public string GetTextPath(string word, Typeface typeface, int fontSize)
{
            FormattedText text = new FormattedText(word,
                new System.Globalization.CultureInfo("zh-cn"),
                FlowDirection.LeftToRight, typeface, fontSize,
                Brushes.Black);

            Geometry geo = text.BuildGeometry(new Point(0, 0));
            PathGeometry path = geo.GetFlattenedPathGeometry();

            return path.ToString();
}
 
用法:
<Path x:Name="textPath" Canvas.Left="10" Canvas.Top="10" Fill="#FFFF0000" />

C#代码:
rootElement.findName('textPath').Data = GetTextPath("测试一下A Test!", "方正大黑简体", 42);

你可能感兴趣的:(WPF中,将普通文字转成路径(Path)的方法)