'Referencing embedded resource images in Windows .NET framework app

I want to deploy a Windows app running on .NET Framework v4.7.2 as a single file executable and embed images used as resources in the executable file. I don't want to migrate the app to .NET Core.

The image file is in the app as Resources\Images\fred.png; I have set the build action of the image file to embedded resource and copy to output directory to do not copy

image properties

I reference this in the code snippet:

var src = new Uri("pack://application:,,,/Resources/Images/fred.png", UriKind.Absolute);

return new BitmapImage(src);

new BitmapImage(src) generates a null exception because the file isn't found.

I have read the documentation here and the uri path provided seems be correct.

I have also read some other SO posts but the either suggestions didn't work, or they weren't trying to embed an image file in an executable.

My question is: how can I load this image file as an embedded resource so I can deploy the application as a single-file executable?



Solution 1:[1]

I changed the build action type from embedded resource to resource and that resolved the issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pgee70