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

31 篇文章 0 订阅
8 篇文章 0 订阅

在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>

记得添加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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF 中可以使用 XAML(eXtensible Application Markup Language)来描述界面布局和应用程序逻辑,因此可以将 WPF 中的控件转换成 XAML 代码。 在 Visual Studio 中,你可以使用 “Document Outline” 窗口来查看和编辑 WPF 窗口中的控件树,并将其转换成 XAML 代码。具体步骤如下: 1. 打开要转换成 XAML 代码的 WPF 窗口。 2. 在 Visual Studio 中打开 “Document Outline” 窗口,可以在菜单栏中选择 “View -> Other Windows -> Document Outline”,或者使用快捷键 “Ctrl + W, U”。 3. 在 “Document Outline” 窗口中选择要转换的控件,右键点击,选择 “Edit Template -> Edit a Copy”。 4. 在弹出的 “Create Style Resource” 窗口中,选择 “Define in” 为 “ResourceDictionary”(这样可以将样式代码保存到 ResourceDictionary 中,以便复用),并为样式命名。 5. 点击 “OK” 按钮,Visual Studio 会自动为你生成样式代码,并打开一个新的 XAML 文件,其中包含了你选择的控件及其样式代码。 你也可以手动将 WPF 控件转换成 XAML 代码。在 XAML 中,你可以使用 `<ControlName>` 标签来创建控件,例如: ``` <Button Content="Click me" Width="100" Height="50" /> ``` 上面的代码将创建一个宽度为100,高度为50的 `Button` 控件,并设置其显示文本为 “Click me”。 如果你要为控件设置样式,你可以使用 `<Style>` 标签来定义样式,例如: ``` <Style TargetType="Button"> <Setter Property="Background" Value="Red" /> <Setter Property="Foreground" Value="White" /> </Style> ``` 上面的代码将为所有 `Button` 控件设置背景色为红色,前景色为白色。 总之,WPF 中的控件可以很方便地转换成 XAML 代码,便于你在开发过程中进行调试和重用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值