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

在WPF中使用

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


用法:


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



在SVG/Silverlight中使用,可以写成 WCF

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
[ServiceContract]
public interface IService
{
    [OperationContract]
    string GetTextPath(FontParam cs);
}


[DataContract]
public class FontParam
{
    string word = "";
    string fontFamily = "宋体";
    int fontSize = 12;

    [DataMember]
    public string Word
    {
        get { return word; }
        set { word = value; }
    }

    [DataMember]
    public string FontFamily
    {
        get { return fontFamily; }
        set { fontFamily = value; }
    }

    [DataMember]
    public int FontSize
    {
        get { return fontSize; }
        set { fontSize = value; }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

using System.Windows.Media;
using System.Windows;

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、服务和配置文件中的类名“Service”。
public class Service : IService
{
    public string GetTextPath(FontParam fp)
    {
        Typeface typeface = new Typeface(new FontFamily(fp.FontFamily), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
        return GetTextPath(fp.Word, typeface, fp.FontSize);
    }

    private 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 System.Windows.Point(0, 0));
        PathGeometry path = geo.GetFlattenedPathGeometry();

        return path.ToString();
    }
}



  
    
      
        
        
      
    
  
  
    
      
        
          
          
          
          
        
      
    
    
  
  
    
  

记得添加PresentationCore.dll和WindowsBase.dll的引用,这两个在.net 4.0 /wpf 的目录下


 ServiceReference1.ServiceClient sc = new ServiceReference1.ServiceClient();
            ServiceReference1.FontParam cs = new ServiceReference1.FontParam();
            cs.FontSize = fontSize.AsInt();
            cs.FontFamily = fontFamily;
            cs.Word = word;
            haha = sc.GetTextPath(cs);

哈哈,这样就可以了!!



来自(http://blog.csdn.net/johnsuna/article/details/1782480)


你可能感兴趣的:(C#,WPF,AspNet,silverlight)