Click here to Skip to main content
15,885,537 members
Articles / Desktop Programming / WPF
Tip/Trick

Vector Icons in WPF

Rate me:
Please Sign up or sign in to vote.
4.97/5 (20 votes)
4 Apr 2016CPOL3 min read 117.6K   2.2K   34   11
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 are 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:

Image 1

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:

Image 2

The path node shown in the above images contains icon data, this will be used in XAML. And there is 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. We can also 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:

XAML
<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.

XAML
<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:

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

Using As ContentControl

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

Using with ViewBox

XAML
<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.

History

  • 4th April, 2016: Initial version

License

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


Written By
Software Developer (Senior) IVY Software Development Services
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInstead of using the path as resource Pin
kobi229416-Jul-18 5:09
kobi229416-Jul-18 5:09 
AnswerRe: Instead of using the path as resource Pin
Sergey Alexandrovich Kryukov6-Dec-22 21:35
mvaSergey Alexandrovich Kryukov6-Dec-22 21:35 
SuggestionAlt solution Pin
King Or22-Dec-16 2:39
King Or22-Dec-16 2:39 
GeneralRe: Alt solution Pin
Sergey Alexandrovich Kryukov4-Sep-21 19:25
mvaSergey Alexandrovich Kryukov4-Sep-21 19:25 
PraiseRe: Alt solution Pin
VR Karthikeyan16-Nov-21 3:25
professionalVR Karthikeyan16-Nov-21 3:25 
BugUnfortunately there is one flaw to this method Pin
Jeff Sn.16-Sep-16 10:10
Jeff Sn.16-Sep-16 10:10 
PraiseRe: Unfortunately there is one flaw to this method Pin
Jeff Sn.16-Sep-16 10:27
Jeff Sn.16-Sep-16 10:27 
SuggestionHowto make vector icons or where to get them Pin
webmaster4424-Apr-16 4:50
webmaster4424-Apr-16 4:50 
PraiseRe: Howto make vector icons or where to get them Pin
VR Karthikeyan4-Apr-16 8:07
professionalVR Karthikeyan4-Apr-16 8:07 
Thank you for your suggestion, I will add tools, websites and steps to get icons. Smile | :)
VR Karthikeyan

SuggestionAdd an image too.. Pin
Afzaal Ahmad Zeeshan4-Apr-16 1:32
professionalAfzaal Ahmad Zeeshan4-Apr-16 1:32 
PraiseRe: Add an image too.. Pin
VR Karthikeyan4-Apr-16 8:09
professionalVR Karthikeyan4-Apr-16 8: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.