-1

I am trying to set an image source in WPF but relative Uri doesn't work.

imgView.Source = new BitmapImage(new Uri("Res/Images/img.png", UriKind.Relative));

But this works:

imgView.Source = new BitmapImage(new Uri("E:/C#/Folder/Project/Res/Images/img.png", UriKind.Absolute));

What should I change in relative Uri?

1 Answers1

0

Set the Build Action of the image to Content and the Copy to Output Directory property to Copy if newer.

This should ensure that it's copied to the output directory of the .exe (typically bin/Debug or bin/Release) when you build. The the path is relative to the .exe and not to the project.

The other option would be to set the Build Action to Resource, which is the default value, and use a pack URI to specify the path.

mm8
  • 163,881
  • 10
  • 57
  • 88