tags:

views:

128

answers:

2

I've got this WPF code which works...

Uri iconUri = new Uri("pack://application:,,,/media/images/VS.ico", UriKind.RelativeOrAbsolute);
this.Icon = BitmapFrame.Create(iconUri);

I'm using a windows forms notifyIcon control in my WPF app, and I now need to assing the Icon to it. How do I get from my WPF icon to a System.Drawing.Icon ?

A: 

Imaging.CreateBitmapSourceFromHBitmap

I use it like:

return Imaging.CreateBitmapSourceFromHBitmap(source.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());

where source is a Bitmap, which you can get by calling your Icon's .ToBitmap() method.

JustABill
I can't see the ToBitmap method anywhere. What class is it a member of? Thanks.
You accepted the other answer, but for completeness, http://msdn.microsoft.com/en-us/library/system.drawing.icon.tobitmap%28v=VS.100%29.aspx . Maybe I'm just confused between the WinForms/WPF Icon classes.
JustABill
A: 

I use the following method:

// Setup the new NotifyIcon
System.Windows.Forms.NotifyIcon notifyIcon = new System.Windows.Forms.NotifyIcon();
notifyIcon.Text = "Name of Application";
notifyIcon.Icon = new System.Drawing.Icon("media/images/VS.ico");
notifyIcon.Visible = true;

Make sure you add a reference to System.Drawing.

Brent
Brent, I can't see the systray icon after running your code although it builds ok. My icon is in a resource. Maybe that's the problem? ... Uri iconUri = new Uri("pack://application:,,,/media/images/VS.ico", UriKind.RelativeOrAbsolute);
My current code looks like this... private void Window_Loaded(object sender, RoutedEventArgs e) { notifyIcon = new System.Windows.Forms.NotifyIcon(); Uri iconUri = new Uri("pack://application:,,,/media/images/VS.ico", UriKind.RelativeOrAbsolute); this.Icon = BitmapFrame.Create(iconUri); // this works so the URI is working ok notifyIcon.Text = "Name of Application"; notifyIcon.Icon = new System.Drawing.Icon("/media/images/VS.ico"); notifyIcon.Visible = true; // nothing shown in systray }
Possibly... The icon I use has a build action of Content, and I copy the icon to the Output directory. I'll update my answer...
Brent
Got it now, thanks to your latest tweaks BUT the path to use is "media\images\VS.ico" without the leading slash. Thanks very much.