在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(); }
用法:
<Path x:Name="textPath" Canvas.Left="10" Canvas.Top="10" Fill="#FFFF0000" />
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(); } }
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0"> <assemblies> <add assembly="PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </assemblies> </compilation> </system.web> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <serviceMetadata httpGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
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)