Download runtime image from resource in WPF

I am trying to load a runtime image in WPF using the following code

_image = new Image(); BitmapImage src = new BitmapImage(); src.BeginInit(); src.UriSource = new Uri(@"pack://application:,,,/images/tagimages/placeholder.png", UriKind.Absolute); src.CacheOption = BitmapCacheOption.OnLoad; src.EndInit(); _image.Source = src; _image.Stretch = Stretch.None; 

In my project, I have a folder with images and a subfolder in this folder called tagimages, which contains placeholder.png. When I run this code, I get the following error:

"Unable to find resources" images / tagimages / placeholder.png '"

What am I doing wrong?

+6
source share
2 answers

Turns out I should have used

 Uri(@"pack://application:,,,/<MyProject>;component/images/tagimages/placeholder.png", UriKind.Absolute); 
+9
source

From the procedural code, you use: @ "pack: // application: ,, / putyourfilenamehere" for the embedded resource.

Or in other words

BitmapImage image = new BitmapImage (new Uri ("pack: // application: ,, / Images / myimage.png"));

+1
source

Source: https://habr.com/ru/post/911182/


All Articles