在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,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),FontStretches.normal); return GetTextPath(fp.Word,fp.FontSize); } private string GetTextPath(string word,int fontSize) { FormattedText text = new FormattedText(word,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,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>
记得添加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);
哈哈,这样就可以了!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。