How to set Startup directory to a Image in xaml - wpf

Sorry for bad describe in the title. I have a image in "C:\Users\aUser\Desktop\Program\Image\Image.png"
But my program is in the same folder with the Image. I can set the Directory manually <Image Source="C:\Users\aUser\Desktop\Program\Image.png" But when the parent directory is moved, the code will no longer work. So how can I set the Source of the image that in the child folder without use of the code behind
Image.Source = new BitmapSource(new Uri(AppDomain.CurrentDomain.BaseDirectory + #"\Image\Image.png")

You will need to be more specific what XAML 'version' you are using (e.g. WPF, UWP or Xamarin.Forms). Anyways, here goes:
UWP
You should read through these docs. Your XAML code could look like this:
<Image Source="ms-appx:///Assets/Image.png"/>
Where Assets/Image.png is a path to your image
Xamarin.Forms
Have a look at these docs. The solution depends on the platform
WPF
Have a look at these docs. Your XAML code could look like this:
<Image>
<Image.Source>
<BitmapImage UriSource="/Images/image.png" />
</Image.Source>
</Image>

Related

RibbonSplitButton cannot locate resource

I have a RibbonSplitButton that contains a SmallImageSource like so:
<UserControl x:Class="myProgram/toolbars/MainToolbar">
<RibbonSplitButton SmallImageSource="/Images/Undo.png"/>
<!-- more buttons-->
</UserControl>
I try to use this in another file (main):
<UserControl x:Class="myProgram/main"
xmlns:toolbar="clr-namespace:myProgram.toolbars">
<toolbar:MainToolbar/>
</UserControl>
I get the error message in main:
Cannot locate resource 'images/undo.png'
However, when I switch to a normal button:
<Button>
<Image Style="{StaticResource buttonstyle}">
<Image.Source>
<BitmapImage DecodePixelWidth="40" UriSource="/Images/Undo.png"/>
</Image.Source>
</Image>
</Button>
the error is gone. I have tried using Pack URIs, changing the Build Action to Resource and Embedded Resource, as well as Clean Soultion, Rebuild Solution, Restarting Visual Studios (rinse and repeat). I image this has something to do with the RibbonSplitButton's image source, but I have no idea. The program runs fine (the Undo.png image shows and works perfectly), it's just annoying to have the error in my developer. Anyone have suggests on how to get rid of the error? (Note, this is a simplified folder structure, for example purposes).
EDIT
For anyone who ends up here at some point in the future, I found that this answer worked.

Using app-level resources in the Generic.xaml file of a custom control library

I have a button on a custom control and I'm trying to display an image on it which is defined as a resource in my App.xaml file like so:
<Application.Resources>
<BitmapImage x:Key="PlusSymbol" UriSource="Resources/PlusSymbol.png" />
</Application.Resources>
For some reason I can't use this as a static resource within my custom control's template defined in the Themes\Generic.xaml file, it crashes my application during runtime saying that it cannot find the requested resource. Here's the xaml code I'm using to bind to the resource:
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
It DOES work during runtime if I define the resource the exact same way but within the Generic.xaml file, however it gives me a pre-compiler warning that it can't find the file since it's now looking for it in Themes/Resources/ rather than just in /Resources/. So I get a warning during design time but it works fine in runtime.
So why can't it find my resource when it's defined in App.xaml? I do this the exact same way in a regular WPF project and it works flawlessly, but in this custom control library it is giving me headaches. Any help is much appreciated!
This should work if you switch your StaticResource to DynamicResource so that the resource will be evaluated dynamically at runtime rather than statically. If you switch to DynamicResource.
<Button Grid.Row="0" Grid.Column="1" Margin="3">
<Image Source="{DynamicResource PlusSymbol}"/>
</Button>
I believe this is because of how theme-based styles and templates are handled, as opposed to standard resources. This answer and this answer speak specifically to Generic.xaml and how it is different from other ResourceDictionaries.
So the way I ended up getting this to work was by defining the BitmapImage in the Generic.xaml file and using a Pack URI to get to the file (here's the MSDN article about Pack URIs, which frankly just confused me). This way it's using a relative path to the file, and specifying the assembly that it's coming from (the file is located at \Resources\PlusSymbol.png in the MyCustomControlLibrary project and has a build action of Resource):
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/MyCustomControlLibrary;component/Resources/PlusSymbol.png" />
Then in the control template in Generic.xaml I use the resource like so:
<Button Grid.Row="0" Grid.Column="1" Margin="3" Height="25" Width="25"
<Image Source="{StaticResource PlusSymbol}"/>
</Button>
Note that I got fooled thinking that I could use the shorter version of the Pack URI like so:
<BitmapImage x:Key="PlusSymbol" UriSource="pack://application:,,,/Resources/PlusSymbol.png" />
However this was still causing the program to crash at runtime. I think the fact that this is a custom control library, and that the end consumer of the image is my UI project, makes the longer version which specifies the assembly that actually contains the image necessary.
Hope that helps anyone else who is having similar problems.
Note I don't think this technically answers my original question which specified using app-level resources (aka defined in the custom control library's App.xaml file) from Generic.xaml. Defining the PlusSymbol resource there still crashes the program. However I didn't actually care about doing it in App.xaml, I was just trying to get it to work right at both design time and run time. This solution does the trick for me, is fairly simple, and from what I can tell from my research is the best practice.

Images bound to images added to resx files using XAML

My WPF application includes a resource file MyResources.resx, containing several strings and images. Because the application will need to be localized, all my references to globalized resources must be made via named properties of the auto-generated MyResources class. The following code works well for string resources:
<Button Content="{x:Static local:Properties.MyResources.ButtonText}" />
However the same does not work for images. Assuming I have an image eflag.bmp added to the resources as a resource named Flag, I would like to be able to do something like this:
<Image Source="{x:Static local:Properties.MyResources.Flag}" />
Please note that the following alternative approach:
<Image Source="/MyNamespace;component/Resources/eflag.bmp" />
cannot be used in this case because it will not be able to handle localization. The problem can be solved using code behind but I am looking for a XAML based solution.
Turn your x:Static into a Binding.Source and add a Converter which does Bitmap to ImageSource.
Source="{Binding Source={x:Static local:Properties.MyResources.Flag},
Converter={StaticResource BitmapToImageSourceConverter}}"
Alternatively you can make the converter a custom markup extension which takes a Bitmap and returns the ImageSource in ProvideValue.
Source="{me:BitmapToImageSource {x:Static local:Properties.MyResources.Flag}}"

accessing image files from a separated assembly

I have several image files I want to share between projects(common icons) I have them in an assembly that would be in every solution I create...I have the files in a folder called Icon and I have the build as content copy always. I have verified that a folder is created with these icons...however my other assemblies are not able to find them...
<r:RibbonGroup Header="Users">
<r:RibbonButton >
<r:RibbonButton.LargeImageSource>
<BitmapImage UriSource="..\Icons\UserIcon.png" />
</r:RibbonButton.LargeImageSource>
</r:RibbonButton>
</r:RibbonGroup>
i have tried formatting the uri several ways...but it never succeeds. If the icons are in the actual assembly though they work...
Try using an absolute Uri. Build action must be set to Resource for UserIcon.png
<BitmapImage UriSource="pack://application:,,,/NameOfImageAssembly;component/Icons/UserIcon.png"/>
Relative Uri should also work
<BitmapImage UriSource="/NameOfImageAssembly;component/Icons/UserIcon.png"/>

How can I set the WPF BitmapImage UriSource property to a relative path?

I'm loading an image in WPF by using the BitmapImage class. My code works perfectly when I give an absolute path for the UriSource but not when I try and use a relative path.
My XAML is:
<Image Width="50" Name="MemberImage" HorizontalAlignment="Left">
<Image.Source>
<BitmapImage DecodePixelWidth="50" UriSource="questionmark.jpg" />
</Image.Source>
</Image>
The questionmark.jpg is added to the project with a Build Action of Resource and Copy to Output Directory set to Copy always. The error I get is "The file [file name] is not part of the project or its 'Build Action' property is not set to 'Resource'". This works when I use an absolute path for the UriSource but that obviously won't do.
How should I be defining my UriSource in the XAML?
I don't think you need to copy the image to output directory once it's in resource.
Correct way to specify is
<BitmapImage
x:Key = "Logo"
UriSource = "pack://application:,,,/ApplicationNamespace;component/Images/App/image.png"
/>
Just replace
ApplicationNamespace with your application namespace
and
Images/App/image.png with your image path in the project
Image files with the following options in properties
Build Action=Content
Copy to Output Directory=Copy if newer
<BitmapImage x:Key="QuestionMark" UriSource="pack://siteoforigin:,,,/questionmark.png"/>
Reference:
Xaml - Bitmap UriSource - absolute path works, relative path does not, why?
I cannot reproduce the problem on my computer:
I add the jpg by choosing Add existing item in the Project menu
I then set its Build Actio to Resource and Copy to Output directory to always.
My XAML is the same.
Works perfectly here, even after moving the .exe and renaming the jpg. Something else must be biting you!

Resources