首页 技术 正文
技术 2022年11月14日
0 收藏 463 点赞 2,556 浏览 2474 个字

保存TextBox中的文字为Path功能

今天再设计一个我自己程序的Icon时使用了Path+textbox做了图形,我不想导出为PNG,因为颜色比较单一,我又想通过代码控制颜色,所以我想完整的保存为Path。所以做了这个软件,支持设置不同的字体和字号,然后点击获取Path,就导出了path。然后粘贴到想用的地方即可。

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;namespace TextTransformationPath
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} [Obsolete]
public string CreateText(string text)
{
System.Windows.FontStyle fontStyle = FontStyles.Normal;
FontWeight fontWeight = FontWeights.Medium;
var pixelsPerDip = VisualTreeHelper.GetDpi(this).PixelsPerDip;
FormattedText formattedText = new FormattedText(
text,
CultureInfo.GetCultureInfo("en-us"),
FlowDirection.LeftToRight,
new Typeface(
new FontFamily(cbo_fontFamily.SelectedItem.ToString()),
fontStyle,
fontWeight,
FontStretches.Normal),
Convert.ToInt32(FontSizeTextBox.Text),
System.Windows.Media.Brushes.Black,
pixelsPerDip
);
// Build the geometry object that represents the text.
var _textGeometry = formattedText.BuildGeometry(new System.Windows.Point(0, 0)); // Build the geometry object that represents the text highlight.
var res = PathGeometry.CreateFromGeometry(_textGeometry).ToString();
return res;
} private void GetTextTransformPath_Click(object sender, RoutedEventArgs e)
{
ResultPathTextBox.Text= CreateText(SourceText.Text);
}
}
}

XAML代码

<Window x:Class="TextTransformationPath.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TextTransformationPath"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBlock Margin="0,5" Text="输入待获取Path的内容:"/>
<TextBox FontSize="{Binding ElementName=FontSizeTextBox,Path=Text}" Height="50" x:Name="SourceText" Text="杜文龙" FontFamily="{Binding ElementName=cbo_fontFamily,Path=SelectedItem}"/>
<ComboBox x:Name="cbo_fontFamily" SelectedIndex="0" ItemsSource="{x:Static Fonts.SystemFontFamilies}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" FontFamily="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsPanel >
<ItemsPanelTemplate >
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<TextBlock Text="字号:"/>
<TextBox x:Name="FontSizeTextBox" Text="30"/>
<Button Margin="0,5" Content="点击获取Path" Click="GetTextTransformPath_Click"/>
<TextBox x:Name="ResultPathTextBox"/>
</StackPanel>
</Grid>
</Window>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,033
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,862