How do I use an icon that is a resource in WPF?

前端 未结 5 507
北荒
北荒 2020-11-29 20:06

I have a .ico file that is embedded as a resource (build action set to resource). I am trying to create a NotifyIcon. How can I reference my icon?

notifyIc         


        
5条回答
  •  被撕碎了的回忆
    2020-11-29 20:59

    Your icon file should be added to one of your project assemblies and its Build Action should be set to Resource. After adding a reference to the assembly, you can create a NotifyIcon like this:

    System.Windows.Forms.NotifyIcon icon = new System.Windows.Forms.NotifyIcon();
    Stream iconStream = Application.GetResourceStream( new Uri( "pack://application:,,,/YourReferencedAssembly;component/YourPossibleSubFolder/YourResourceFile.ico" )).Stream;
    icon.Icon = new System.Drawing.Icon( iconStream );
    

提交回复
热议问题