一般文字をパス(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をテストして!」
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の参照を追加してください.この2つは.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)