How does one change title bar image in WPF Window?
Asked Answered
G

7

54

How does one change the titile bar image (the top-left most icon) in WPF?

Grunberg answered 24/2, 2011 at 8:3 Comment(0)
E
56

The Icon attribute of Window is used to set Icon of a 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" >

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);

You can read more from here

Erigena answered 24/2, 2011 at 8:7 Comment(0)
O
20

Easy way to add image to title bar:

In your Project, Select - Properties - Application - Resources - Icon and Manifest - select the .ico image(always convert your image to .ico)

Add this line(icon) in WPF Main window:

Title="xxxxx" **Icon="xxxxxx.ico"**>
Osculation answered 27/7, 2016 at 8:15 Comment(1)
R click on project in the solution explorer menu > properties > application > resourcesOrgell
S
10
<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>

or in code

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

Source: Window.Icon Property

Spoilfive answered 24/2, 2011 at 8:26 Comment(0)
A
4

This one worked for me (using Visual Studio 2017)

  • Select menu Project/[yourprojectname] Properties
  • Click Application tab (its the first one on top)
  • Here you can browse for Icon, and it will be copied to your project
Abagail answered 13/10, 2018 at 15:11 Comment(0)
B
3
  1. Add ico file to project Resources and check as Embedded Resource
  2. Set Project->Properties->Icon and choose from Resources
  3. Run Project in Release Mode or start without debugging.
Brazilin answered 2/9, 2015 at 12:35 Comment(3)
While these few steps was working for me, I am still puzzled why the default icon is showing in debug.Esterify
These steps don't make any sense in the context of visual studio. How do you add an icon to project "Resources"? Where is this "Properties->Icon" menu that you refer to? Etc.Swagger
R click on project in the solution explorer menu > properties > application > resourcesOrgell
C
2

(1) When the icon is in the project root (same directory as the *.csproj file) and the XAML file is in a sub directory, then a leading slash is needed.

<Window ... Icon="/myIcon.ico"> ... </Window>

Similar for projects, where the icon is in the Resources sub directory.

<Window ... Icon="/Resources/myIcon.ico"> ... </Window>

(2) The icon file has to be included to the project (visible in Project Explorer). Also check in the file properties of the Project Explorer that the build type is set to Resource. (is the default setting)

Chaudoin answered 20/11, 2023 at 16:50 Comment(0)
S
1
 <Window Icon="youricon.ico"></Window>
Salesgirl answered 24/4, 2017 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.