Windows Icon in WPF


An Icon is a bitmap image (.ico file) that is displayed in the top left corner of a Window. Visual Studio designer allows you to create your own icon images. To add an icon to a project, right click on

the project name in Solution Explorer. Right click will open Add Items menu.  Select Add >> New Item.

Now on Installed Templates, select Icon File (see Figure 1) and click Add. This action adds Icon1.ico file and opens the image editor.

WPFIconImg1.gif

Figure 1

In the image editor (see Figure 2), you design your icon the way you like. There are two icon sizes available in the editor - 16x16 and 32x32.

 WPFIconImg2.gif

Figure 2

The Icon attribute of Window is used to set Icon of a window. Listing 1 sets Icon1.ico file as icon of Window.

<Window x:Class="WindowSample.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="WPF Window Sample" Height="350" Width="525"

        Name="FirstWindow" Icon="Icon1.ico" >

Listing 1

The output with new icon looks like Figure 3.

WPFIconImg3.gif

Figure 3

The Icon property of Window class represents a window's icon at run-time. This property takes an ImageSource variable.

The following code snippet uses BitmapFrame.Create method to create an ImageSource and sets the Icon property of a Window.

Uri iconUri = new Uri("pack://application:,,,/Icon1.ico", UriKind.RelativeOrAbsolute);

this.Icon = BitmapFrame.Create(iconUri);


Summary


In this article, I demonstrated how to use Icon property of a Window to load an icon in a WPF application.


erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all