Embedded images not showing when in a UserControl - wpf

I have an Image control that contains a path to an embedded image (build action 'resource').
<Image Source="Assets/images/image.png" Stretch="None" />
If I add that to a container in my MainPage.xaml the image appears fine. When having the same image in a UserControl as shown below, and then adding an instance of that UserControl on MainPage.xaml the image does not appear.
<UserControl x:Class="HomePage.Views.SimpleUserContol"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Grid x:Name="LayoutRoot" >
<Image Source="Assets/images/image.png" Stretch="None" />
</Grid>
</UserControl>
Can anyone shed any light on why this happening and maybe point me in the direction of a solution.
Cheers, J
(I'm working in Silverlight but would think the same thing probably happens in WPF)
EDIT:
Setting
<Image Source="/Assets/images/image.png" Stretch="None" />
works fine when setting the build action to 'Content' however it doesn't work when using 'resource'. The problem is definatly it's relative position in the file structure as add ../ works fine. I'd still like a solution to get an image from the assembly if possible

You have to reference it as a resource, not just the path.
This is how it is done in a WPF application:
<Image Source="/MyAppName;component/images/image.png" Stretch="None" />
The original image is located in images/image.png
Note:
I have no experience in SilverLight, but you said it is probably similiar in WPF, so I suggest this...

You're using a relative path to the image. If your UserControl is located in a subdirectory, the relative path is not valid anymore. You should use an absolute path like "/Assets/images/image.png", or "pack://application:,,,/Assets/images/image.png" (use this last version if your UserControl is going to be used by another assembly)

If you needed to change its build type from Content to Resource, try building clean. I had everything exactly right (path to other project's resources, build type, et al.) yet it didn't work until I added a new image to the images folder, which perhaps cleared out the old statuses.
In fact, manually delete the bin and obj folders in the project where the image is.

I think the problem is related to the "virtual" namespace your image got when it's embedded in ressources (from the logical path to it) and the difference with your usercontrol namespace.

using a '/' to get to the root of the site works only if the root of the site is not within a subdirectory. ie: an admin site as a subdirectory to the main site (http://www.somesite.com/admin). In this case using '/assets/images/image.png' would then go to the parent site first. You may be able to reference the image like so: '~/assets/images/image.png'

I've also found the when an image has special characters that require encoding in the file name, like "+", which gets encoded %2b can cause the problem as well.
I suggest renaming any images that might have contain escape characters.

I had this problem and I tried moving the image in and out of the Canvas, and up and down the Canvas container. I even tried manipulating the panel.zindexes to no avail. I was using source= "/images/map.jpg". The image folder was created by right clicking the project and adding folder, so it was below the resources folder.
Solved: I navigated to the images folder in windows explorer and used alt+d, and ctrl+c to copy the file location with these \ instead of these / and voila it worked.

Related

Image path for ribbon application item

I got little problem with my image path for ribonn aplicaton menu item.
I got this piece of code:
<ribbon:RibbonApplicationMenuItem x:Name="MenuItemLogout" Header="Odhásit"
ImageSource="/Resources/logoutSmall.png"
Command="{Binding RelogUserCommand}"/>
The thing i want is: the right path of image. Because in this case, Visual Studio cant find that path. This are two different project. The one where i using this ribbon is called Z05.WPF.WeighingSystem in folder 01_LoadingPlace/ViewAnnouncedVehicles - in this folder i have particular xaml and view model.
And i have another project called Z05.DesktopApplication where i have folder called Resources and there i have all of my images. I also tried to use this path:
pack://application:,,,/Resources/
But its also doesnt work.
Any tips? Thanks! :)

Could not find a part of path when using ImageBrush

I get that error if i try to run my "application". In fact that's an empty window with background.
<Window x:Class="NWN_Tsuki.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="831" Width="875">
<Window.Background>
<ImageBrush Stretch="None" ImageSource="pack://siteoforigin:,,,/Resources/Book.png" />
</Window.Background>
<Grid/>
</Window>
That's all my app. And i get error. If i set absolute path to image - all working fine.
I tried to google, i searched same topics here and tried to do what worked for others - there are no progress. Just one window. Just one background image in resourses added through wizard.
Visual Studio 2013, Windows 7 Ultimate x64
I haven't tried adding an image thru a wizard, what I do is create a "Resources" folder in my project. Then I add the image in said folder. Set the property of the image to Copy Always or Copy If New. Then do:
<ImageBrush Stretch="None" ImageSource="../Resources/Book.png" />
If anything, Book.png is just not getting copied.
EDIT: IMPORTANT Also make sure to change the Book.png's Build Action to Resource
First of all, I think you can just write ImageSource="Resources/Book.png" and it will work.
Second, if you want with "pack://", you suppose to write it ImageSource="pack://application:,,,/Resources/Book.png"
Yet another way - ImageSource="ProjectName;component/Resources/Book.png" where ProjectName is the name of the project where you keep you picture.
You can even concatenate all this if you need ImageSource="pack://application:,,,/ProjectName;component/Resources/Book.png"
I have experienced this. It turned out, the .png was corrupted. When I navigated to the folder in File Explorer, I saw the following
I think the mkelem is something to do with our source control, so maybe it was that which corrupted it, regardless, this is why some people may get this error message.

How to combined multiple PATH objects into a vector image that can be used as a style in WPF/XAML

I have a complex logo developed in Illustrator that I want to use as an image in WPF. I can seem to combine the Path's when importing into Blend to get a single Path without loosing all the fill colours.
What is the best way to convert the illustrator image into something that can be used in WPF/XAML.
Usually i just combine the Path's into a single Path and then reference the path using the following:
<Application.Resources>
<Path x:Key="pathKeyName" Data="..."/>
</Application.Resources>
<Path Style="{StaticResource pathKeyName}" />
However there seems to be no easy way to group multiple paths together into a single image that can be used as a resource in a similar manner.
Perhaps there is a way to convert these paths into a Geometry ?
EDIT
In the meantime I have used Blend to create a UserControl from the selected paths and then I place this control inside a Viewbox - not quite as simple but it seems to work.
No sweat, go grab yourself Mike Swanson's AI to XAML exporter plugin for Illustrator (dont pay attention to the versions mentioned on there, it works just fine with the newest version I use it all the time) and export your logo to XAML for WPF.
Then take that XAML and plop it in a ContentControl like I show how on a previous SO answer post.
Then you plop that Style Template into your resource dictionary or wherever and reference it as such with one line wherever/however you need it, voila. :)
Hope this helps, cheers.
OH PS, if there's a lot of shapes/colors etc, you'll never get it into just one Path, sorry amigo.

How do you store an image in a Silverlight 3 custom control?

I have created a custom control that will have a bitmap image in it. My project structure is:
Solution
ProjectName
Resources
Actor.png
The XAML I am using is:
<Image x:Name="ActorBitmap" Source="ProjectName;component/Resources/Actor.png />
However, this is not working. I have the build options on the picture set to "Resource" and "Copy Always". Can someone please explain what I'm doing wrong ?
Thanks,
Scott
I believe you just need a leading forward slash before the resource location:
<Image x:Name="ActorBitmap" Source="/ProjectName;component/Resources/Actor.png />
There is also no need to set copy to output directory to "Copy Always". Because the build action is set to Resource the image data will be in the assembly itself, and you will not be referencing it from the output directory.

In WPF how do I specify the path to a file nested in a Directory using XAML?

I am trying to do this...
<Image x:Name="imgGroupImage" Source="Images\unlock.png" Margin="0,0,5,0" />
But I get this error...
Cannot convert string 'Images\unlock.png' in attribute 'Source' to object of type 'System.Windows.Media.ImageSource'. Cannot locate resource 'forms/images/unlock.png'. Error at object 'System.Windows.HierarchicalDataTemplate' in markup file 'Fuse;component/forms/mainwindow.xaml' Line 273 Position 51.
As you can see, my form that includes this XAML is in a folder named Forms. My Images are in a folder named Images. How do I map from Forms to Images?
I tried Source="..Images\unlock.png" which does not work in WPF.
Any help?
Try slashes rather than backslashes, and use an absolute path by leading with a slash:
Source="/Images/unlock.png"
That generally works for me.
Failing that, take a look at Pack URIs.
Add your image to the Project in VS
Right click on that image unlock.png
Go to Context menu /Properties
Change Build Action to Resource
Thats it :-)
Have you tried setting the source to a BitmapImage?
<Image x:Name="imgGroupImage" Margin="0,0,5,0" >
<Image.Source>
<BitmapImage UriSource="Images/unlock.png" />
</Image.Source>
</Image>
I believe the default type of Uri for UriSource is a relative Uri, which works off the application's base class. You might find you can configure the BitmapSource a bit easier than trying to find the exact way you have to enter the file path in the Source attribute.
In order to use resource located in folder different that the one where your XAML is, do this:
<Image Source="pack://application:,,,/Resources/image.png"/>
Where Resources is name of directory which you want to use resources from and image.png is name of image to display. Answer found thanks to #matt-hamilton and #brian-hinchey and their mention of Pack URI.
Works perfectly with your own Converters. You just have to return string matching schema above.

Resources