ttom
ttom

Reputation: 1065

How do I have VS recognize an existing icon?

VS 2019, I add an existing icon (x.ico) --- Properties => Resources.resx => (right click) Open => Resources.resx. --- I drag and drop x.ico into Resources.resx.

The x.ico now appears under Resources.resx\Resources.Designer.cs\Resources.

I place the x.ico name into:

<Window x:Class="x.MainWindow"
   ...
   Icon="x.ico"
>

This is how I've done it in the past.

There are no errors when I compile. However when I run it => IOException: Cannot locate resource 'x.ico'.

Upvotes: 1

Views: 49

Answers (2)

ttom
ttom

Reputation: 1065

There is no need to convert the .ico to .png. This works:

Project Name (right click) => Properties => Application (left side) => Icon => Browse to the existing .ico.

Upvotes: 0

Keithernet
Keithernet

Reputation: 2509

The reason why this isn't working is that WPF doesn't like ICO files. If you want to use ICOs, you will need to load the ICO from resources in code, convert it, and then set the Icon property in code.

However, if you change your icon into a PNG, then your original approach will work.

Your XAML will look like this:

Icon="Resources/x.png"

And like the commenters mentioned, make sure that x.png in the Resources folder has its build action set to Resource.

Upvotes: 1

Related Questions