Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a System.Windows.Media.ImageSource From an Icon File in c#?

Tags:

c#

wpf

I am programming in a WPF application in c#. I need to change the notifier icon sometimes;

I implemented the icon like this:

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

My solution is changing the Icon, the type of MyNotifyIcon.Icon is ImageSource, and I want to get by an icon file. I could find the way to do that.

Do somebody have some ideas how? Or have the any other solution?

Shortly, I have an address like /Resource/logo.icon, and I wanna get a System.Windows.Media.ImageSource

like image 329
cindywmiao Avatar asked May 22 '14 21:05

cindywmiao


2 Answers

You can use the BitmapImage class:

        // Create the source
        BitmapImage img = new BitmapImage();
        img.BeginInit();
        img.UriSource = new Uri("./Resource/logo/logo.icon");
        img.EndInit();


BitmapImage class inherits from ImageSource class which means you can pass the BitmapImage object to NotifyIcon.Icon as:


NI.Icon = img;
like image 50
George Chondrompilas Avatar answered Oct 15 '22 01:10

George Chondrompilas


Are you using the NotifyIcon from the Hardcodet.Wpf.TaskbarNotification namespace created by Philipp Sumi? If so you have the option to either specify the icon as an ImageSource or an Icon.

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

Note that internally setting IconSource sets Icon.

To set from a resource.

notifyIcon.Icon = MyNamespace.Properties.Resources.SomeIcon
like image 26
denver Avatar answered Oct 15 '22 02:10

denver