Why is my Image unable to locate the Resource? - wpf

I'm stumped. I am trying to display an image embedded in another assembly. I've tried several things, but nothing has worked so far. I'm convinced I'm missing something really simple.
Currently, the Xaml looks something like this:
<Image>
<Image.Source>
<BitmapImage UriSource="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
</Image.Source>
</Image>
I originally had Xaml that looked like this:
<Image Source="pack://application:,,,/Name.Of.Assembly;component/Resources/Icons/icon.png" />
I switched it hoping it would make a difference. (It didn't.)
I have a reference to Name.Of.Assembly in the project I am trying to create an image. In the assembly, Name.Of.Assembly, there's a directory called Resources, which has a subfolder called Icons, which contains the image icon.png. The build action on icon.png is set to Embedded Resource.
When I try to run, I get a XamlParseException:
'Initialization of 'System.Windows.Media.Imaging.BitmapImage' threw an exception'
Whose inner exception is:
'Cannot locate resource 'resources/icons/icon.png'
It's like it's not looking in the assembly for the resource. Why does it fail to load the image? What am I missing?

I found a solution to my problem:
The first issue was probably having my build action set to Embedded Resource instead of Resource. Once I changed this I ran a Build on the project containing the resource, but I noticed nothing different.
Per Sean Nelson's suggestion, I did a Clean, followed by a Rebuild on the project.
The image is now displaying.

Related

Set default xaml window background image in dll

I am currently developing a default WPF control Kit.
But I am stuck with using the correct kind of uri in xaml.
What I have is an image wich should be used as the background for the non-client area of my window.
To make the default controls available very easy I want to put everything in a dll.
Other apps can quickly reference that dll and get access to the style.
The problem is, that my image is not showing up when using the dll style in an app.
My image (/Resources/WindowBackground.jpg) is set to Resource and I am using the following xaml:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99">
<Image.OpacityMask>
<ImageBrush ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
</Image.OpacityMask>
</Image>
I also tried:
<Image Grid.ColumnSpan="99" Grid.RowSpan="99" Source="/Resources/WindowBackground.jpg"/>
Both write the following into the output (Couple times):
..."System.IO.IOException" in PresentationFramework.dll...
I also tried lots of other uris wich sometimes lead to XamlParseExeptions and other not so nice stuff.
Thank you for any hints :D
You can find your answer in the Pack URIs in WPF page on MSDN. For your particular situation, you can use the following syntax to reference your resource image file:
pack://application:,,,/ReferencedAssembly;component/Resources/WindowBackground.‌​jpg
From the linked page:
The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.
pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml
Note: The type of resource file here is irrelevant.

Refer an image resource from Universal App shared project

I just started work with winRT, shared projects etc. and faced with some problems.
I want to put all of my resources (strings, images etc.) in one project (portable class library or shared project).
When I put an image in PCL everything works fine in xaml by referencing with ms-appx:
<ImageBrush ImageSource="ms-appx:///Resources/Images/baby.jpg" Stretch="Uniform"/>
But when I put a string resource in PCL, I have following weird xaml behavior with ResourceDictionary:
http://postimg.org/image/ysbkvv6d7/
Ok. Then I decided to put every resources in Shared project. For this time it works perfectly with strings but fails with images: I couldn't get the right URI string in ImageSource of ImageBrush.
So the questions are:
How can I add ResourceDictionary in PCL
What is the correct URI format to reference image from shared project.
Thanks in advance!
To access an image your shared project file, just use the direct file path. The
<Image Source="Assets/ImageName.png"></Image>
Edited by #JerryNixon
<Image Source="ms-appx:///Assets/ImageName.png"></Image>
To access an image in your PCL, use the longer syntax
<Image Source="ms-appx:///ProjectName/...path.../"/>

WPF Centralize xaml/image Resources in Multi-Tier/Project Application Solution

My situation is this.
I have multiple projects under a WPF solution which make up a multi-tier application I am developing. Since separate projects will sometimes need to access the same image or xaml resource I'd like to centralize "Public" resources to one project that can be referenced by any other projects requiring these "Public" resources. For example, I have a BMP image that I use in various controls and windows in separate projects that serves as a banner/logo. It seems like I should be able to add this image as a resource to the public resources project and reference it from my other projects instead of adding the image separately to every project that needs it. If this is possible, what would it look like and how should I go about doing it? If my idea just sucks I'm open to suggestions, but the project is going to be quite large so I don't want to be adding resources all over the place.
Thanks!
P.S.
I have searched this topic quite a bit but there are so many garbage answers out there from people that don't know what they are doing. Given that I'm relatively new to WPF I'd rather get a direct answer to my problem.
Well after some tinkering and research I found a solution to my own question. It seems like bits and pieces of my answer were scattered all over the web so I'll try to explain the whole thing at once, in one place. To re-cap I basically wanted to centralize any resources used across projects within my solution. These are the steps I eventually followed to accomplish this.
1.
Create a User Control project in your solution (any project that can host Resource Dictionaries will do). I'll refer to this project as "the resource project".
2.
Add any resource files (images etc...) that you want to share between projects to the resource project. I try to organize files in sub-directories that make sense. You will want to set the build action to "Resource" so that it gets compiled into the output binary.
3.
Now add a resource dictionary to the resource project. In my case I wanted to reference several images so I made one called "ImageDictionary.xaml".
4.
Add references to the image files in the resource dictionary. I am referencing images here but its just a resource dictionary, you can put whatever in there. The string in the middle is just the relative path to the image file you are referring to.
<ImageSource x:Key="SomeImageKey">
Images/SomeImage.bmp
</ImageSource>
5.
Now go to a project that requires the resource dictionary you just made in step 4. It can be another user control or a window, whatever. Right-click on project references and add a reference to the resource project.
6.
Okay now the current project needs to reference the dictionary you made in step 4 that is defined in the resource project. I made a user control first so this is what my code would look like below... Note that I used merged dictionaries. I plan on using more later so I chose this way instead of a single dictionary.
"ResourceProject" below is the name of the project/assembly that you added the resource to.
"component" is a keyword that needs to be there, followed by the path to the dictionary xaml code file.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceProjectAssembly;component/Resources/ImageDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
As you can see, referencing an external resource dictionary requires something called a "Pack URI". I found a short hand way of doing it. The long way is quite a bit uglier and as far as I know, there is no advantage to it. But here is what the long way looks like.
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ResourceProjectAssembly;component/Resources/ImageDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
7.
Now that your current project has a reference to the resource dictionary in the resource project, you can use anything from that dictionary. I went ahead and put an image in my user control. Below is the code. Its just like referencing a dictionary locally.
<Image Source="{StaticResource SomeImageKey}" />
8.
Important Note!
This caused me a headache for a while before I found it. Make sure that the startup project in your solution has a reference to the resource project EVEN IF THE STARTUP PROJECT DOES NOT USE THE RESOURCE PROJECT. (I.e. right-click references, add reference etc...)
The resources will not get compiled into the output unless you do this. This part was tricky because the designer was even showing my images and I had no xaml errors but at runtime it would throw set property and cannot find file exceptions. This went away when I referenced my resource project from my startup project.
Hopefully this helps somebody.
Below are some links to a few places (including stackoverflow) that gave me what I needed to put all the pieces together.
http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx
Load WPF styles or other Static Resources from an external file or assembly
ResourceDictionary in a separate assembly

XamlParseException on setting image from resources

i got some trouble. I just implemented the Microsoft Ribbon Framework into my application. Now i wanted to set an icon for one button. So i wrote:
<Ribbon:RibbonButton Header="Create Project" LargeImageSource="pack://application:,,,/Resources/document_empty.png" />
But everytime i start the app, i got an "XamlParseException" in this line, with the inner exception: "File not found".
Why the hell is this so? I don´t understand, why it doesn´t find the image. It´s just placed in the resources of the assembly. The designer show´s it and the code doesn´t get highlighted as it was an syntax error or something like that...
Anyone who has this problem too?
Thanks!
As you haven't specified exactly, I assume you are using the Properties\resources.resx part of your application? If so this is not the desired way for a WPF application.
See this post on SO
Instead create a folder in your assembly, place your images in there and set its build action to resource.
See picture:
In your Markup use the packUri:
pack://application:,,,/images/IMG.jpg

Why can't I set the Image Source in WPF app?

I'm just learning WPF. I'm using VS 2008. I've added an Image control to my XAML, and I added a GIF file to the project. If I use the XAML Designer's property panel to set the image's Source property, the drop-down list for the Source property contains the following:
C:\Sample Project;component/pinHorizontal.gif
There are several problems with this:
My project, named "Sample Project," is not in the root of my drive.
Why is ";component" in there?
If I select this value as given, I get the error "Property value is not valid" (yeah, no kidding).
If I go into the XAML source and set the Source property manually, like so:
<Image Name="PinImage" Source="pinHorizontal.gif"/>
The XAML Designer gives me this error:
"The file pinHorizontal.gif is not part of the project or its 'Build Action' property is not set to 'Resource'."
Why is this task so difficult? How do I assign an image source?
Have a read through this article - though be warned it's not particularly light reading :-)
I suspect what you want for your image path is pack://application:,,,/pinHorizontal.gif. if your image is set to a BuildAction of Resource, this will work fine.
Right click on the image file(pinHorizontal.gif) in the SolutionExplorer, Go to properties, here is the 'Build Action' property, you need to set it to 'Resource'. Hope it helps!!
This post is an "and also"... not an answer to the original question.
I just thought I'd document it in case some other poor booger has this problem in future... and googling that error message finds this SO question... so here's a good place to do so.
I've been creating new icons (actually bitmaps) and adding them "on the fly" to my Visual Studio 2008 project (i.e. Add ~ Existing item). The IDE has been reporting file ${filename} is not part of the project or its 'Build Action' property is not set to 'Resource' errors on my XAML page... but my project builds and runs OK... so what gives???
I tried everything I could think of the get visual studio to refresh it's view of the filesystem, to no avail.
Well, I've just dicovered that restarting Visual Studio "refeshes" its cache of filesystem/build-output contents, which makes those annoying non-errors in the IDE go away. So I add a batch of icons, mark them all as Resources ~ Copy if newer, build, and then restart the IDE, and it's all good.
Cheers all. Keith.
In the Solution Explorer, select your project and right click rebuild the solution, and try to add the image again, it should work fine.
--or--
under the Build tab select rebuild the solution, and try to add the image again.
You can use this piece of code in the XAML to load an image from the absolute path without setting nothing. Use DecodePixelWidth or DecodePixelHeight to save application memory.
<Image>
<Image.Source>
<BitmapImage DecodePixelWidth="200" UriSource="C:\image.png" />
</Image.Source>
</Image>
Hope it helps!
I was having the exact same problem. I just manually typed in the location of the file when the Choose image box pops up and that worked for me.
I opened the Choose image box again after I got it to work and noticed that file:/// was now in front of the file location. I know next to nothing about Visual Basic (which is what I'm using) so there may be a good explanation for this, but I don't have it.

Resources