How to set window icon in code behind in wpf? How to set window icon in code behind in wpf? wpf wpf

How to set window icon in code behind in wpf?


Something like

myWindow.Icon = new BitmapImage(new Uri("/VBDAdvertisement;component/Images/logoVBD.png"));

You may need to qualify the path more though.

Edit: As i thought the path should be in pack-uri format:

"pack://application:,,,/VBDAdvertisement;component/Images/logoVBD.png"


This is the correct way to do it (assuming MyIcon.ico is placed on the root folder of a WPF project named MyApplication):

Uri iconUri = new Uri("pack://application:,,,/MyApplication;component/MyIcon.ico");myWindow.Icon = BitmapFrame.Create(iconUri);

This is also what actually happens when you set the Icon property for the window in XAML.

When just setting the Icon to a new Bitmap, it will not be rendered smoothly and correctly, but instead quite a bit pixelated.


Try this its absolutely working for both png as well as ico image format.

window.Icon = BitmapFrame.Create(Application.GetResourceStream(new Uri("LiveJewel.png", UriKind.RelativeOrAbsolute)).Stream);