如何使用GeometryDrawing?

时间:2011-06-26 02:15:35

标签: c# wpf xaml text geometrydrawing

我想用GeometryDrawing画出A~Z这个词......

有人可以教我这个吗?

感谢...

1 个答案:

答案 0 :(得分:3)

以下是使用GeometryDrawing绘制文字的示例。

XAML文件Window1.xaml:

<Window x:Class="TextDrawing.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="150" Width="300">
    <Canvas>
        <Image Stretch="None" HorizontalAlignment="Left" Margin="10">
            <Image.Source>
                <DrawingImage>
                    <DrawingImage.Drawing>
                        <GeometryDrawing x:Name="geoDrawing"/>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Image.Source>
        </Image>
    </Canvas>
</Window>

C#文件Window1.xaml.cs:

using System.Windows;
using System.Windows.Media;
using System.Globalization;

namespace TextDrawing
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            FormattedText atoz = new FormattedText("ABC...XYZ",
                CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                new Typeface("Arial"), 50.0, Brushes.Black);
            Geometry geo = atoz.BuildGeometry(new Point(0, 0));
            geoDrawing.Geometry = geo;
            geoDrawing.Pen = new Pen(Brushes.Black, 1.0);
            geoDrawing.Brush = Brushes.Yellow;
        }
    }
}

结果输出:

Text rendered with GeometryDrawing