6

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

cindywmiao
  • 937
  • 2
  • 11
  • 26

3 Answers3

3

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;
George Chondrompilas
  • 3,167
  • 27
  • 35
1

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
denver
  • 2,863
  • 2
  • 31
  • 45
0

have you thought in do it with resources?

To get from resources:

MainNameSpace.Properties.Resources.NameOfIconFileInResources;

Put the icons in resources, if you have the image file (not icon) i have a method to change it:

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

And programmatically change it with the known attribute .Icon; Don't worry about ImageSource type.

Extracting image from icon (.ico) file:

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

But you must put the .ico file in resources, or take it with relative path...

Taken from How can you access icons from a Multi-Icon (.ico) file using index in C#

Community
  • 1
  • 1
DrkDeveloper
  • 939
  • 7
  • 17
  • Shortly, I have a string like **./Resource/logo/logo.icon**, I want turn into a type of **Windows.Media.ImageSource** not **Icon** – cindywmiao May 22 '14 at 22:24
  • Put it in resources! After: ImageSource heritage: ImageSource -> BitmapSource -> Bitmap. You want to do Image from Icon and then cast (or during the process). Icon has method called ToBitmap; – DrkDeveloper May 22 '14 at 22:29
  • @DrkDeveloper You can't cast from an Icon to an ImageSource. – denver Feb 11 '15 at 16:41