0

I have a valid multi-size .ico image file in the Resource directory of my VS C# .NET6 WPF project. This resource file is marked as Embedded resource to be included in the binary.

I try to explicitly reference it with

Uri iconUri = new Uri("pack://application:,,,/Resources/iconfile.ico");
this.Icon = BitmapFrame.Create(iconUri);

There is no compilation errors in the project. This second line gives a runtime error in VS 2022 debugger: System.IO.IOException: Cannot locate resource 'resources/iconfile.ico'.

Please could you help me understand why this resource component is not visible to the code.

ChatGPT and forum archives didn't produce any explanation for this.

P.S. It doesn't seem to complain about the neighbour .png file (also marked as embedded resource), which I refer in an XAML file directly with a similar URI as a Source for <Image>. Initially I referred to the .ico file also in my XAML to assign it as an Icon in <Window>, but I got the same runtime error for InitialiseComponent(); so I decided to refer to it explicitly in .cs and got the same effect.

I also noticed that my .ico file is PNG-based for the 256x256 size and BMP-based for all other sizes... Can this have anything to do with the issue?

Alex
  • 707
  • 1
  • 4
  • 9
  • The code you use works for `Resource`, not `Embedded Resource`. To load an Embedded Resource, use [Assembly.GetManifestResourceStream](https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getmanifestresourcestream) – Klaus Gütter Jul 25 '23 at 05:26

2 Answers2

1

Multi sized icon files need to be 16x16, 32x32, 48x48 and 256x256 pixels and require at least one blank pixel around the outer edge of entire image enclosed within. Check to make sure that your icon is not touching one of the outer edges of the grid and that all sizes are using the same format (PNG, BMP, etc.)

Consider running your source images through an online ICO convertor to validate and ensure that the icon in correct.

https://convertio.co/png-ico/

Infinyte
  • 51
  • 5
  • Thank you @Infinyte. I converted my png with this tool, replaced the file in the resources, marked this new one as `embedded resource`. Sadly the runtime error is the same :-( The previous one was obtained by me with `magick iconfile.png -resize 256x256 -gravity center -extent 256x256 -define icon:auto-resize="256,128,96,64,48,32,16" iconfine.ico` – Alex Jul 24 '23 at 22:37
1

In short, you need to mark your file with the "Resource" build action, not "Embedded resource".

Embedded resources are for manifest resources, that you load with methods like Assembly.GetManifestResourceStream(). It's a different thing.

For WPF to load a resource from a pack URI, it needs to be built with the Resource build action.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • Thank you very much. I intended to include this .ico file into the .exe binary, and thought that the only way to do so is to mark is as "Embedded resource" (chatGPT implied that), but now I checked with chatGPT again and it no longer insists it's the only way. :-) – Alex Jul 25 '23 at 18:36
  • 1
    @Alex one more proof that Generative AI like ChatGPT is making everything worse. These models have no idea what they are talking about, but make you think they do due to the way they talk. Their output sounds smart or intelligent, but when it comes to answering questions about programming, they are a total failure. We see this all the time here on StaclOverflow. – Clemens Jul 26 '23 at 11:17