保存TextBox中的文字为Path功能

保存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>

posted @ 2021-06-20 00:42  杜文龙  阅读(208)  评论(0编辑  收藏  举报