How to set window icon in code behind in wpf?

不问归期 提交于 2020-05-11 04:03:27

问题


In xaml it is :

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

I want to convert it into code behind.

Thanks


回答1:


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"



回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/8254281/how-to-set-window-icon-in-code-behind-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!