在C#中如何从图标文件创建System.Windows.Media.ImageSource?

6

我是一名使用C#编写WPF应用程序的开发人员。有时需要更改通知图标;

我实现了以下代码:

<tn:NotifyIcon x:Name="MyNotifyIcon" 
               Text="Title" 
               Icon="Resources/logo/Error.ico"/>

我的解决方案是更改图标,MyNotifyIcon.Icon 的类型是 ImageSource,我想通过一个图标文件来获取它。我找到了这样做的方法。

有人有什么想法吗?或者有其他的解决方案吗?

简单地说,我有一个地址,像/Resource/logo.icon,我想得到一个System.Windows.Media.ImageSource

3个回答

3
您可以使用BitmapImage类:
        // Create the source
        BitmapImage img = new BitmapImage();
        img.BeginInit();
        img.UriSource = new Uri("./Resource/logo/logo.icon");
        img.EndInit();

BitmapImage类继承自ImageSource类,这意味着您可以将BitmapImage对象作为参数传递给NotifyIcon.Icon,如下所示:


NI.Icon = img;

你不知道 .ico - 多图标文件 - (或 .icon - 单色图标文件 -,它没有解释)中是否有一个或多个图标... 我不知道这段代码是否会抛出异常。 - DrkDeveloper
@GeorgeChondrompilas,image的类型是什么? - cindywmiao
这是一个 Image 类的对象(WPF 控件)。很抱歉,您不需要它。 - George Chondrompilas
当我使用NI.Icon = img时,会抛出异常。NI.icon = (System.Windows.Media.ImageSource)img也是如此。 - cindywmiao
System.Windows.Markup.WpfXamlParseException: "不支持的URI语法。该方法期望相对URI或pack://application:,,,/形式的绝对URI。" - cindywmiao
显示剩余5条评论

1

您是否正在使用由Philipp Sumi创建的Hardcodet.Wpf.TaskbarNotification命名空间中的NotifyIcon?如果是,则可以选择将图标指定为ImageSource或Icon。

TaskbarIcon notifyIcon = new TaskbarIcon();
// set using Icon
notifyIcon.Icon = some System.Drawing.Icon; 
// set using ImageSource
notifyIcon.IconSource = some System.Windows.Media.ImageSource; 

请注意,内部设置IconSource会设置Icon。
从资源中设置。
notifyIcon.Icon = MyNamespace.Properties.Resources.SomeIcon

请注意,只有 IconSource 属性是可绑定的。 - g t

0
你有考虑过用资源来实现吗?
从资源中获取:
MainNameSpace.Properties.Resources.NameOfIconFileInResources;

将图标放在资源文件夹中,如果您只有图片文件(而非图标),我有一种方法可以更改它:

    public static Icon toIcon(Bitmap b)
    {
        Bitmap cb = (Bitmap) b.Clone();
        cb.MakeTransparent(Color.White);
        System.IntPtr p = cb.GetHicon();
        Icon ico = Icon.FromHandle(p);
        return ico;
    }

使用已知属性.Icon以编程方式更改它; 不必担心ImageSource类型。

从图标(.ico)文件中提取图像:

Stream iconStream = new FileStream (MainNameSpace.Properties.Resources.NameOfIconFileINResources, FileMode.Open );
IconBitmapDecoder decoder = new IconBitmapDecoder ( 
    iconStream, 
    BitmapCreateOptions.PreservePixelFormat, 
    BitmapCacheOption.None );

// loop through images inside the file
foreach ( var item in decoder.Frames )
{
 //Do whatever you want to do with the single images inside the file
 this.panel.Children.Add ( new Image () { Source = item } );
}

// or just get exactly the 4th image:
var frame = decoder.Frames[3];

// save file as PNG
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(frame);
using ( Stream saveStream = new FileStream ( @"C:\target.png", FileMode.Create ))
{
    encoder.Save( saveStream );
}

但你必须将.ico文件放在资源中,或者使用相对路径获取它...

摘自C#中如何通过索引访问多图标(.ico)文件中的图标


简而言之,我有一个字符串,类似于 ./Resource/logo/logo.icon,我想将其转换为 Windows.Media.ImageSource 类型,而不是 Icon - cindywmiao
把它放在资源里!之后:ImageSource 继承自 BitmapSource -> Bitmap。你想从图标创建 Image,然后进行转换(或在过程中)。图标有一个叫做 ToBitmap 的方法。 - DrkDeveloper
@DrkDeveloper 你无法将 Icon 强制转换为 ImageSource。 - denver

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接