Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set window icon in code behind in wpf?

In xaml it is :

  <View:BaseWindow.Icon>
    /VBDAdvertisement;component/Images/logoVBD.png
  </View:BaseWindow.Icon>

I want to convert it into code behind.

Thanks

like image 759
JatSing Avatar asked Nov 24 '11 08:11

JatSing


People also ask

How to set icon in WPF application?

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.


2 Answers

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"
like image 61
H.B. Avatar answered Sep 22 '22 09:09

H.B.


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.

like image 28
Christian Myksvoll Avatar answered Sep 20 '22 09:09

Christian Myksvoll