18

In xaml it is :

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

I want to convert it into code behind.

Thanks

JatSing
  • 4,857
  • 16
  • 55
  • 65
  • Does this answer your question? [How to change title bar image in WPF Window?](https://stackoverflow.com/questions/5101895/how-to-change-title-bar-image-in-wpf-window) – StayOnTarget Mar 19 '20 at 14:27

3 Answers3

27

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"
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • yes, it is : myWindow.Icon = new BitmapImage(new Uri("pack://application:,,,/VBDAdvertisement;component/Images/logoVBD.png")); Thanks :) – JatSing Nov 24 '11 at 09:26
  • To avoid the ugly "pack..." strings have a look at https://stackoverflow.com/questions/1127647/convert-system-drawing-icon-to-system-media-imagesource – oo_dev Dec 02 '19 at 13:55
11

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.

Christian Myksvoll
  • 634
  • 2
  • 7
  • 16
8

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);
Joee
  • 1,834
  • 18
  • 19