Click here to Skip to main content
12,622,652 members (29,246 online)
Click here to Skip to main content
Add your own
alternative version

Stats

9.6K views
181 downloads
21 bookmarked
Posted

Vector Icons in WPF

, 4 Apr 2016 CPOL
Rate this:
Please Sign up or sign in to vote.
Vector icons in WPF

Introduction

Icons are very important in effective user interface design, which improves user experience with the software and establishes an intuitive connection between the software and its users. We are mostly using image files (.jpg, .png, .bmp) as icons, for different places, we are using different sized image files, for example, we are using small size (16x16) image as favicon, medium size (32x32) as desktop icon, and we also using icons in sizes like 48x48 and 128x128, etc.,

Why Vector Icons?

Using image files (.jpg, .png, .bmp) as icons has some issues. We can't use the same image for different scales, we have to maintain different sized versions for the same image, so the number of files and icon sizes are increased, also we need to crop the images in different sizes. And sometimes, images files cannot be displayed clearly.

So, in order to overcome these problems, we can go for vector icons. Vector icons can be resized to any size without loss in quality of the image. Also vector icons are stored as mathematical data.

Getting Vector Icons

We can draw our own vector icons using popular vector drawing tools like Inkscape, Expression Design, or Adobe Illustrator. There are lot of tutorials and documentation is available for these tools, which will help to get started with these tools. After the designing of icons using these tools, you can copy the generated mathematical data for the icons, and use it in wpf applications. For example, the below GIF shows how to get the mathematical data of drawn icon from Inkscape,

If you don't want to create a new vector icon, you can use existing vector icons, you can get vector icons for free or you can get paid version of icons, there are lot of sites providing free vector icons, some of them below,

Just download icons from above sites, each icon may have .svg file or .eps file, just open this file in notepad and check for the path node, copy the icon data and paste it in xaml file. Or you can get the icon data from the website itself, Material Design Icons provides the xaml version of icons in the website itself, see the below image,

The path node shown in the above images contains icon data, this will be used in xaml. And there is an another free tool available for vector icon design, i.e., Syncfusion Metro Studio - a tool for creating vector icons, which is loaded with huge collection of icons in vector format, and we can customize it, also we can see the xaml part for the customised vector icons, this tool is a very very useful and creative tool.

How Can I Use It In WPF?

WPF supports vector based images. We can define vector icons in XAML and we can use it with WPF controls.

Step 1

Create a Resource Dictionary in WPF project. Name it as Icons.xaml or whatever name you want.

Step 2

Many icon design softwares and icon developing sites are providing free vector icons, we can get vector format for those icons, for example, refer to Material Design Icons, they are providing icons in XAML format. Just copy path node of the XAML part of the icon.

Step 3

To add an icon in Icons.xaml, just paste the copied path node like below and name it:

<Path x:Key="SettingsIcon" Data="M12,15.5A3.5,3.5 0 0,1 8.5,12A3.5,3.5 0 0,1 12,
8.5A3.5,3.5 0 0,1 15.5,12A3.5,3.5 0 0,1 12,15.5M19.43,12.97C19.47,12.65 19.5,12.33 19.5,12C19.5,
11.67 19.47,11.34 19.43,11L21.54,9.37C21.73,9.22 21.78,8.95 21.66,8.73L19.66,5.27C19.54,5.05 19.27,
4.96 19.05,5.05L16.56,6.05C16.04,5.66 15.5,5.32 14.87,5.07L14.5,2.42C14.46,2.18 14.25,2 14,2H10C9.75,
2 9.54,2.18 9.5,2.42L9.13,5.07C8.5,5.32 7.96,5.66 7.44,6.05L4.95,5.05C4.73,4.96 4.46,5.05 4.34,
5.27L2.34,8.73C2.21,8.95 2.27,9.22 2.46,9.37L4.57,11C4.53,11.34 4.5,11.67 4.5,12C4.5,12.33 4.53,
12.65 4.57,12.97L2.46,14.63C2.27,14.78 2.21,15.05 2.34,15.27L4.34,18.73C4.46,18.95 4.73,19.03 4.95,
18.95L7.44,17.94C7.96,18.34 8.5,18.68 9.13,18.93L9.5,21.58C9.54,21.82 9.75,22 10,22H14C14.25,22 14.46,
21.82 14.5,21.58L14.87,18.93C15.5,18.67 16.04,18.34 16.56,17.94L19.05,18.95C19.27,19.03 19.54,
18.95 19.66,18.73L21.66,15.27C21.78,15.05 21.73,14.78 21.54,14.63L19.43,12.97Z" Fill="Black" />

Step 4

Merge Icons.xaml resource dictionary with our WPF application like below.

Open App.xaml file and merge the Icons.xaml file inside Application.Resources tag.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary x:Name="IconsDictionary" 
            Source="pack://application:,,,/VectorIconsInWPF;component/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

Step 5

Now we are having vector icon in our Icons.xaml file, to use it in our WPF application, there are different ways available. Let's see what they are.

Using With Rectangle

We can use our vector path inside a rectangle like:

<Rectangle Width="100" Height="100" Fill="Blue">
    <Rectangle.OpacityMask>
        <VisualBrush Stretch="Fill" 
Visual="{StaticResource SettingsIcon}" />
    </Rectangle.OpacityMask>
</Rectangle>

Using As ContentControl

<ContentControl Content="{StaticResource SettingsIcon}" Margin="0" />

Using with ViewBox

<Viewbox Width="48" Height="48">
    <Canvas Width="24" Height="24">
        <Path Data="M12,15.5A3.5,3.5 0 0,1 8.5,12A3.5,3.5 0 0,1 12,8.5A3.5,
        3.5 0 0,1 15.5,12A3.5,3.5 0 0,1 12,15.5M19.43,12.97C19.47,12.65 19.5,12.33 19.5,
        12C19.5,11.67 19.47,11.34 19.43,11L21.54,9.37C21.73,9.22 21.78,8.95 21.66,8.73L19.66,
        5.27C19.54,5.05 19.27,4.96 19.05,5.05L16.56,6.05C16.04,5.66 15.5,5.32 14.87,5.07L14.5,
        2.42C14.46,2.18 14.25,2 14,2H10C9.75,2 9.54,2.18 9.5,2.42L9.13,5.07C8.5,5.32 7.96,
        5.66 7.44,6.05L4.95,5.05C4.73,4.96 4.46,5.05 4.34,5.27L2.34,8.73C2.21,8.95 2.27,
        9.22 2.46,9.37L4.57,11C4.53,11.34 4.5,11.67 4.5,12C4.5,12.33 4.53,12.65 4.57,
        12.97L2.46,14.63C2.27,14.78 2.21,15.05 2.34,15.27L4.34,18.73C4.46,18.95 4.73,
        19.03 4.95,18.95L7.44,17.94C7.96,18.34 8.5,18.68 9.13,18.93L9.5,21.58C9.54,
        21.82 9.75,22 10,22H14C14.25,22 14.46,21.82 14.5,21.58L14.87,18.93C15.5,
        18.67 16.04,18.34 16.56,17.94L19.05,18.95C19.27,19.03 19.54,18.95 19.66,
        18.73L21.66,15.27C21.78,15.05 21.73,14.78 21.54,14.63L19.43,12.97Z" 
        Fill="Black" />
    </Canvas>
</Viewbox>

So we can use vector icons inside WPF application by using the above methods, now we can change the color and size of these vector icons without losing quality.

Refer to the attached project for more details.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

VR Karthikeyan
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
BugUnfortunately there is one flaw to this method Pin
Member 1028235816-Sep-16 11:10
memberMember 1028235816-Sep-16 11:10 
PraiseRe: Unfortunately there is one flaw to this method Pin
Jeff Sn.16-Sep-16 11:27
memberJeff Sn.16-Sep-16 11:27 
SuggestionHowto make vector icons or where to get them Pin
webmaster4424-Apr-16 5:50
memberwebmaster4424-Apr-16 5:50 
PraiseRe: Howto make vector icons or where to get them Pin
VR Karthikeyan4-Apr-16 9:07
professionalVR Karthikeyan4-Apr-16 9:07 
SuggestionAdd an image too.. Pin
Afzaal Ahmad Zeeshan4-Apr-16 2:32
mvpAfzaal Ahmad Zeeshan4-Apr-16 2:32 
PraiseRe: Add an image too.. Pin
VR Karthikeyan4-Apr-16 9:09
professionalVR Karthikeyan4-Apr-16 9:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161128.1 | Last Updated 5 Apr 2016
Article Copyright 2016 by VR Karthikeyan
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid