Im trying to load a image at runtime 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 called images and a sub folder of that folder called tagimages that contains the placeholder.png. When I run this code I get the following error
"Cannot locate resource 'images/tagimages/placeholder.png'"
What am I doing wrong?
It turns out that I should have used
Uri(@"pack://application:,,,/<MyProject>;component/images/tagimages/placeholder.png", UriKind.Absolute);
From procedural code you use: @"pack://application:,,,/putyourfilenamehere" for an embedded resource.
Or in other words
BitmapImage image = new BitmapImage(new Uri("pack://application:,,,/Images/myimage.png"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With