0

I want something simple - display an image in WPF, both from XAML and from Code-Behind.

My raw materials are .png, .bmp, and sometimes .gif.

Until now I loaded them in many different ways, for example:

<Image Source="../../Resources/Images/close_btn.png"></Image>

and

MyImage = new BitmapImage(new Uri($"pack://application:,,,/Resources/Images/file.png"));

but now I want to put everything in resources.resx, and load it from there. But here's the problem. Image Source = requires a string, not a resourced image. Same for BitmapImage - it requires a Uri, and not the actual resource.

Is there a simple way to display the images? (i.e. not use converters, streams etc.)

Maverick Meerkat
  • 5,737
  • 3
  • 47
  • 66
  • Try this: [*Pack URI to image embedded in a resx file*](https://stackoverflow.com/q/16409819/109702) and [*Access ResourceDictionary items programmatically*](https://stackoverflow.com/q/2032294/109702). – slugster Sep 14 '17 at 12:11
  • @slugster - this is too complicated. If that is the only option, I will just leave my images as they are and use the Uri's and relative paths. I don't really understand why would I want to put images in Resources, if I can't really use them in any practical way... – Maverick Meerkat Sep 14 '17 at 12:18
  • 1
    You put your images in resources if you want to swap them out for branding or regionalisation or cultural reasons (having them compiled into resources makes it easy to swap the libraries based on region/culture, you can also merge the appropriate resource dictionary at app startup time). – slugster Sep 14 '17 at 12:49
  • 1
    A WPF application usually stores images as *assembly resources*, i.e. image files in the root- or sub-folder of a Visual Studio project, with their Build Action set to Resource. They are loaded by Resource File Pack URIs, either in XAML or in code behind. – Clemens Sep 14 '17 at 13:27
  • @slugster Clemens ok, understood. Since I don't have any need for localization for my images now, I will leave them as image files in directories and load them with URI. – Maverick Meerkat Sep 14 '17 at 13:48

0 Answers0