views:

2436

answers:

2

I am using an icon in my application in two situations.

  1. In XAML as an Image for the Button using DynamicResource.
  2. In C# as NotifyIcon

    this.notifyIcon.Icon = new SystemDrawing.Icon("..//..//Shutdown.ico" );

My problem is if I delete the image the application not working. How can I bind the image with the exe file so that the application can run only with the exe file?

+2  A: 

Add the icon to the project as a Resource (not Embedded Resource, there is a difference), then access it with

using(Stream stream = Application.GetResourceStream(new Uri("/Shutdown.ico")).Stream)
{
    this.notifyIcon.Icon = new SystemDrawing.Icon(stream);
}

You should also then be able to use it in your XAML too, I just can't remember how yet.

See here for more information: http://msdn.microsoft.com/en-us/library/aa970494.aspx

Note: I have not included any error checking here. You should probably make sure you check the stream to see if it is null before using it.

Jamie Penney
When I use I got an errorError 1 Cannot implicitly convert type 'System.Windows.Resources.StreamResourceInfo' to 'System.IO.Stream'
Sauron
I missed the .Stream call on the end of the GetResourceStream() call. Did you look at StreamResourceInfo to see where I went wrong? You aren't going to get anywhere just copy pasting code without figuring out how it works.
Jamie Penney
A: 

The last time I needed to do this, the image was so simple that it was easiest to simply create a line drawing in XAML (similar to SVG) instead of using a file. I was attempting to build a simple Vista-style wizard without reusing anyone else's code (to see if I could), and I wanted the arrow of a command button as can be seen in the UX guidelines on MSDN.

IanGilham