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

           ,               

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

           

    原文作者:何必当初女
    原文地址: https://blog.csdn.net/qq_44919342/article/details/89842615
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞