apc
apc

Reputation: 5566

WPF - Using a XAML Icon/Vector file

I have download the ModernUI icons as found here http://modernuiicons.com/ an one of the formats is xaml.

For example the appbar.printer.text.xaml file contains:

    <?xml version="1.0" encoding="utf-8"?>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_printer_text" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
    <Path Width="44" Height="45" Canvas.Left="16" Canvas.Top="17" Stretch="Fill" Fill="#FF000000" Data="F1 M 25,27L 25,17L 51,17L 51,27L 47,27L 47,21L 29,21L 29,27L 25,27 Z M 16,28L 60,28L 60,51L 52,51L 52,46L 55,46L 55,33L 21,33L 21,46L 24,46L 24,51L 16,51L 16,28 Z M 25,39L 28,39L 28,52L 35,52L 35,59L 48,59L 48,39L 51,39L 51,62L 33,62L 25,54L 25,39 Z M 46,55L 38,55L 38,52L 46,52L 46,55 Z M 46,49L 30,49L 30,46L 46,46L 46,49 Z M 46,43L 30,43L 30,40L 46,40L 46,43 Z "/>
</Canvas>

Is there any way to use it as is by referencing the file or do I have copy and modify he content into a resource dictionary?

Upvotes: 2

Views: 695

Answers (1)

keymusicman
keymusicman

Reputation: 1291

There are several ways to do what you want.

You can add this xaml file to resx resource file. Then you can load it with XamlReader like that:

    void Load()
    {
        using (Stream s = GenerateStreamFromString(Properties.Resources.appbar_printer_text))
        {
            UIElement element = (UIElement)XamlReader.Load(s);
            // do what you want with this element
        }
    }

    Stream GenerateStreamFromString(string s)
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(s);
        writer.Flush();
        stream.Position = 0;
        return stream;
    }

If you want to use only Path from this file as source for Image, you can add DrawingImage to your ResourceDictionary:

<DrawingImage x:Key="appbar_printer_text">
    <DrawingImage.Drawing>
        <DrawingGroup>
            <DrawingGroup.Children>
                <GeometryDrawing Brush="#FF000000" Geometry="F1 M 25,27L 25,17L 51,17L 51,27L 47,27L 47,21L 29,21L 29,27L 25,27 Z M 16,28L 60,28L 60,51L 52,51L 52,46L 55,46L 55,33L 21,33L 21,46L 24,46L 24,51L 16,51L 16,28 Z M 25,39L 28,39L 28,52L 35,52L 35,59L 48,59L 48,39L 51,39L 51,62L 33,62L 25,54L 25,39 Z M 46,55L 38,55L 38,52L 46,52L 46,55 Z M 46,49L 30,49L 30,46L 46,46L 46,49 Z M 46,43L 30,43L 30,40L 46,40L 46,43 Z ">
                    <GeometryDrawing.Pen>
                        <Pen LineJoin="Round" Brush="#FF000000"/>
                    </GeometryDrawing.Pen>
                </GeometryDrawing>
            </DrawingGroup.Children>
        </DrawingGroup>
    </DrawingImage.Drawing>
</DrawingImage>

<Image Source="{StaticResources appbar_printer_text}" />

So, you just have to move Data from Path to Geometry.

Something like that.

Upvotes: 1

Related Questions