如何从BitmapImage创建NotifyIcon?

时间:2013-05-18 10:24:50

标签: c# wpf c#-4.0 bitmapimage notifyicon

我的资源字典中有一个资源项(png文件),我在几个地方使用它。现在我想将它用作应用程序的NotifyIcon。但我不知道该怎么做。你有什么想法吗?

// my image:

var bitmap = new BitmapImage();
bitmap.UriSource = new Uri("pack://application:,,,/MyProj.Resources;component/Icons/Logo_48x48.png");

// and I try to do this:

var iconHandle = bitmap.GetHicon(); // but this line doesn't work
var ni = new NotifyIcon {
    Icon = System.Drawing.Icon.FromHandle(iconHandle),
    Visible = true
};

1 个答案:

答案 0 :(得分:3)

var sri = Application.GetResourceStream(
    new Uri("pack://application:,,,/MyProj.Resources;component/Icons/Logo_48x48.png"));
var bitmap = new Bitmap(sri.Stream);
var handle = bitmap.GetHicon();
var ni = new NotifyIcon {
    Icon = System.Drawing.Icon.FromHandle(handle),
    Visible = true
};