Sauron
Sauron

Reputation: 16903

Embed Icon in WPF application

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?

Upvotes: 1

Views: 7877

Answers (2)

Jamie Penney
Jamie Penney

Reputation: 9522

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

using (var stream = Application.GetResourceStream(new Uri("pack://application:,,,/Shutdown.ico")).Stream)
{
    this.notifyIcon.Icon = new System.Drawing.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: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/wpf-application-resource-content-and-data-files

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.

Upvotes: 3

Ian Gilham
Ian Gilham

Reputation: 1954

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.

Upvotes: -1

Related Questions