Window.Icon Property (System.Windows)

Switch View :
ScriptFree
.NET Framework Class Library
Window.Icon Property

Updated: August 2010

Gets or sets a window's icon.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
Syntax

Visual Basic
Public Property Icon As ImageSource
	Get
	Set
C#
public ImageSource Icon { get; set; }
Visual C++
public:
property ImageSource^ Icon {
	ImageSource^ get ();
	void set (ImageSource^ value);
}
F#
member Icon : ImageSource with get, set

Property Value

Type: System.Windows.Media.ImageSource
An ImageSource object that represents the icon.
Dependency Property Information

Identifier field

IconProperty

Metadata properties set to true

None

Remarks

Windows Presentation Foundation (WPF) standalone applications have two types of icons:

  • One assembly icon, which is specified by using the <ApplicationIcon> property in the application's project build file. This icon is used as the desktop icon for an assembly.

    Note Note

    When debugging in Visual Studio, your icon may not appear due to the hosting process. If you run the executable, the icon will appear. For more information, see Hosting Process (vshost.exe).

  • One icon per window that is specified by setting Icon. For each window, this icon is used in its title bar, its task bar button, and in its ALT-TAB application selection list entry.

A WPF window always displays an icon. When one is not provided by setting Icon, WPF chooses an icon to display based on the following rules:

  1. Use the assembly icon, if specified.

  2. If the assembly icon is not specified, use the default Microsoft Windows icon.

If you use Icon to specify a custom window icon, you can restore the default application icon by setting Icon to null.

A single icon can be used in a variety of ways in Windows, including being shown in the title bar of a window, the task bar for a window, the ALT+TAB file selection list. Each of these shows the icon using a different size; a 16x16 pixel icon is displayed in the title bar of a window and in the task bar, while a 32x32 pixel icon is displayed in the ALT+TAB file selection list. Some applications, like Microsoft Windows Explorer, provide a View menu that allows you to choose the size of the icon you'd like to view.

To cater to the various display sizes, an icon file is composed of one or more actual icons where each one represents a version of the icon that targets a specific size and color depth. For example, one icon may only have a single 16x16 pixel icon with 16 colors, while another may contain 16x16 pixel and 32x32 pixel icons with both 16 colors and 256 colors.

If icons for all possible sizes and color depths exist within an icon file, Window will use the appropriate icon. If an icon file contains only a subset of all the possible icons, Window uses the next most appropriate icon in order of decreasing sizes and color depth.

The result is that an icon will always be used by Window, although the icon used may not target the required size and color depth. For example, a 16x16 pixel icon with 16 colors may be used for display as a 32x32 pixel icon with 256 colors. This can cause undesirable visual effects, such as pixilation, but may be avoided by creating icons for all targeted sizes and color depths.

Note Note

You cannot set or get this property when a window is hosted in a browser.

Examples

The following example shows how to set a window icon.

XAML

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.MainWindow"
	Icon="WPFIcon1.ico">
</Window>


Visual Basic

' Set an icon using code
Dim iconUri As New Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute)
Me.Icon = BitmapFrame.Create(iconUri)


C#

// Set an icon using code
Uri iconUri = new Uri("pack://application:,,,/WPFIcon2.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);


Version Information

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
.NET Framework Security

  • UIPermission  

    for permission to set the icon. Associated enumeration: AllWindows

Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
See Also

Reference

Other Resources

Change History

Date

History

Reason

August 2010

Added a note about the hosting process.

Customer feedback.

Community Content

SimonRev
Actually it does, but not in the debugger
Just like the documentation states -- the vshost process interferes with assembly icon resolution, but if you run your app standalone it does use the assembly icon.

pcfountain
Does not use assembly icon
I have found that the XAML window does not use the assembly icon, as the help file states.  Does anybody know how to set the icon property to a project resource?  Not in a resource dictionary, but a traditional project resource.