如何在 WPF 中使用作为资源的图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/74466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:36:38  来源:igfitidea点击:

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

wpfresourcesicons

提问by ScottG

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?

我有一个作为资源嵌入的 .ico 文件(构建操作设置为资源)。我正在尝试创建一个 NotifyIcon。如何引用我的图标?

notifyIcon = new NotifyIcon();
notifyIcon.Icon = ??     // my icon file is called MyIcon.ico and is embedded

回答by user13125

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:

您的图标文件应该被添加到您的项目程序集中之一,并且它的构建操作应该设置为资源。添加对程序集的引用后,您可以创建一个 NotifyIcon,如下所示:

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

回答by Thomas Bratt

A common usage pattern is to have the notify icon the same as the main window's icon. The icon is defined as a PNG file.

一个常见的使用模式是让通知图标与主窗口的图标相同。该图标被定义为一个 PNG 文件。

To do this, add the image to the project's resources and then use as follows:

为此,请将图像添加到项目的资源中,然后按如下方式使用:

var iconHandle  = MyNamespace.Properties.Resources.MyImage.GetHicon();
this.notifyIcon.Icon = System.Drawing.Icon.FromHandle(iconHandle);

In the window XAML:

在 XAML 窗口中:

<Window x:Class="MyNamespace.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:Seahorse"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="600"
Icon="images\MyImage.png">

回答by Jaykul

Well, you don't want to use the resx style resources: you just stick the ico file in your project in a folder (lets say "ArtWork") and in the properties, set the Build Action to "Resources" ...

好吧,您不想使用 resx 样式资源:您只需将项目中的 ico 文件粘贴到文件夹中(比如“ArtWork”),然后在属性中将构建操作设置为“资源”...

Then you can reference it in XAML using PACK URIs ... "pack://application:,,,/Artwork/Notify.ico"

然后您可以使用 PACK URI 在 XAML 中引用它......“pack://application:,,,/Artwork/Notify.ico”

See here: http://msdn.microsoft.com/en-us/library/aa970069.aspxand the sample

请参阅此处:http: //msdn.microsoft.com/en-us/library/aa970069.aspx示例

If you want to be a little bit more ... WPF-like, you should look into the WPF Contribproject on CodePlex which has a NotifyIcon control which you can create in XAML and which uses standard WPF menus (so you can stick "anything" in the menu).

如果你想多一点...... WPF-like,你应该看看CodePlex 上的WPF Contrib项目,它有一个 NotifyIcon 控件,你可以在 XAML 中创建它并使用标准的 WPF 菜单(所以你可以坚持“任何"在菜单中)。

回答by blackSphere

I created a project here and used an embedded resource (build action was set to Embedded Resource, rather than just resource). This solution doesn't work with Resource, but you may be able to manipulate it. I put this on the OnIntialized() but it doesn't have to go there.

我在这里创建了一个项目并使用了嵌入式资源(构建操作设置为嵌入式资源,而不仅仅是资源)。此解决方案不适用于 Resource,但您可以对其进行操作。我把它放在 OnIntialized() 上,但它不必去那里。

//IconTest = namespace; exclamic.ico = resource 
System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream("IconTest.Resources.exclamic.ico");

   if (stream != null)
   {
       //Decode the icon from the stream and set the first frame to the BitmapSource
       BitmapDecoder decoder = IconBitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.None);
       BitmapSource source = decoder.Frames[0];

       //set the source of your image
       image.Source = source;
    }

回答by Mike Sage

If you are just looking for the simple answer, I think this is it where MyApp is your application name and where that's the root namespace name for your application. You have to use the pack URI syntax, but it doesn't have to be that complicated to pull an icon out of your embedded resources.

如果您只是在寻找简单的答案,我认为这就是 MyApp 是您的应用程序名称以及您的应用程序的根命名空间名称的地方。您必须使用 pack URI 语法,但从嵌入的资源中提取图标并不需要那么复杂。

    <Window x:Class="MyApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Height="100"
    Width="200"
    Icon="pack://application:,,,/MyApp;component/Resources/small_icon.ico">