在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);
哈哈,这样就可以了!!
来自(http://www.voidcn.com/article/p-rdtkyzub-yk.html)
总结以上是内存溢出为你收集整理的将普通文字转成路径(Path)的方法 (WPF,Silverlight,SVG)全部内容,希望文章能够帮你解决将普通文字转成路径(Path)的方法 (WPF,Silverlight,SVG)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)