How to get a Uri of the image stored in the resources
In a WPF application you would usually not store images in Properties/Resources.resx
and access them by means of the Properties.Resources
class.
Instead you just add the image files to your Visual Studio project as regular files, perhaps in a folder named "Images" or the like. Then you would set their Build Action
to Resource
, which is done in the Properties window. You get there e.g. by right-clicking the image file and select the Properties
menu item. Note that the default value of the Build Action
should be Resource
for image files anyways.
In order to access these image resources from code you would then use a Pack URI. With the above folder name "Images" and an image file named "LedGreen.png", creating such an URI would look like this:
var uri = new Uri("pack://application:,,,/Images/LedGreen.png");
So you could perhaps declare your property to be of type Uri:
public Uri ImageUri { get; set; } // omitted INotifyPropertyChanged implementation
and set it like this:
ImageUri = resultInBinary.StartsWith("1")
? new Uri("pack://application:,,,/Images/LedGreen.png")
: new Uri("pack://application:,,,/Images/LedRed.png");
Finally your XAML should look like shown below, which relies on built-in type conversion from Uri to ImageSource:
<Grid>
<Image Width="10" Source="{Binding Path=ImageUri}" />
</Grid>
Get the URI of an image stored in drawable
You should use ContentResolver
to open resource URIs:
Uri uri = Uri.parse("android.resource://your.package.here/drawable/image_name");
InputStream stream = getContentResolver().openInputStream(uri);
Also you can open file and content URIs using this method.
How to make Uri reference to a Resource image embedded in the assembly itself?
The other answer is valid but doesn't produce a BitmapSource.
The correct answer, given that I have a folder called Images and inside an Image with the Build Action set to Resource, is to use a Resource File Pack URI:
_myIcon = new BitmapImage(new Uri("pack://application:,,,/Images/MyImage.png");
How to Set Image Resource URI from Code-Behind
Part of the problem is that WPF has no context with which to resolve that URL. It's a relative URL, and typically, it would be resolved relative to the base URI of the XAML content in which it's used. If I use exactly the same URL you start with in this code:
public MainWindow()
{
InitializeComponent();
var img = new Image();
Content = img;
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(@"/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png", UriKind.RelativeOrAbsolute);
bmp.EndInit();
img.Source = bmp;
img.Width = bmp.PixelWidth;
}
then it works. That's in the codebehind for MainWindow
, obviously.
With one tiny change, moving this line:
Content = img;
to the end, then I get the same DirectoryNotFoundException
as you.
WPF tries to resolve that URI to an actual resource at the point at which you assign the BitmapImage
as the Source
property of that Image
. My first example works because the Image
is in the visual tree, and so it picks up the base URI of MainWindow.xaml
, and resolves that resource URI relative to that base URI.
If you really need to create the Image
before it gets associated with a visual tree, you've got various options. You could actually set the base URI on the image:
img.SetValue(BaseUriHelper.BaseUriProperty, baseUri);
However, that's kind of weird. It's easier just to construct an absolute URI, e.g.:
bmp.UriSource = new Uri(
baseUri,
@"/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png");
Both of these of course presume that you know what the base URI is. You can find that out by asking in your MainWindow constructor:
public MainWindow()
{
InitializeComponent();
var baseUri = BaseUriHelper.GetBaseUri(this);
...
In your case, that'll be: pack://application:,,,/ImageResTest;component/mainwindow.xaml
This in turn makes it clear what the resolved URI should be: pack://application:,,,/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png
Interestingly, you say you try that and get an error. Well I'm trying that exact URI, and I'm not getting an error. Just to be clear, here's my modified version of your MyClass
constructor:
public MyClass(Uri baseUri)
{
img = new Image();
var bmp = new BitmapImage();
bmp.BeginInit();
bmp.UriSource = new Uri(baseUri, @"/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png");
bmp.EndInit();
img.Source = bmp;
img.Width = bmp.PixelWidth;
}
and here's my MainWindow
constructor:
public MainWindow()
{
InitializeComponent();
var obj = new MyData.SomeStuff.MyClass(BaseUriHelper.GetBaseUri(this));
this.Content = obj.Img;
}
This works for me, having followed your instructions. If I understand you correctly, you're seeing a FileNotFoundException
when you do this. This makes me wonder if your instructions have omitted something. E.g., I'd expect to see this error if ImageResTestLib
was strongly named. (If you want to refer to a resource in a strongly-named library, you need a fully qualified assembly display name before the ;component
part.)
Another option would be to use Application.GetResourceStream
, along with the BitmapImage.StreamSource
property. But again, this is going to need a working URL, so you're likely going to hit the same problem as you had before. Once you work out what's different in your project that's stopping pack://application:,,,/ImageResTestLib;component/MyData/SomeStuff/Resources/Img.png
from working, then the basic approach you already have should be fine.
Load image from resource and apply to Image.source
As per MSDN exmaple you should do like this -
Image myImage3 = new Image();
BitmapImage bi3 = new BitmapImage();
bi3.BeginInit();
bi3.UriSource = new Uri("smiley_stackpanel.PNG", UriKind.Relative);
bi3.EndInit();
myImage3.Stretch = Stretch.Fill;
myImage3.Source = bi3;
https://msdn.microsoft.com/en-us/library/system.windows.controls.image.source(v=vs.110).aspx
Accessing embedded image and creating a System.Windows.Controls.Image
Related Topics
Getting Specified Node Values from Xml Document
Getting Attribute Value of an Xml Document Using C#
Repository Pattern Step by Step Explanation
Httpclient Not Supporting Postasjsonasync Method C#
How to Stop Flickering C# Winforms
Access to Foreach Variable in Closure Warning
Webapi Streamcontent VS Pushstreamcontent
Updating an Observablecollection in a Separate Thread
How to Read a File Which Is in Use
Serialize a Container of Enums as Strings Using JSON.Net
Use Reflection to Invoke an Overridden Base Method
Capture Multiple Key Downs in C#
Nunit VS. Visual Studio 2008's Test Projects for Unit Testing
Convert String to Nullable Type (Int, Double, etc...)
How to Get the Word Under the Cursor in Windows
What's the Difference Between Application.Run() and Form.Showdialog()