In the WinForms project i have a resource file Resources.resx where i have a resource image Image1.
It is saved as
<data name="Image1" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\Image1.tif;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
Now i'll get the runtime path to my resource Property.Resources.Image1. I know how i get a bitmap. But i need the runtime path to this resource by key (Image1) or just the value in xml file.
Related
to make my Application localizable I use a method from this blog:
http://www.rhyous.com/2010/10/20/using-resources-resx-for-strings-in-a-wpf-application-a-technique-to-prepare-for-localization/
So i have controls defined like this to get the strings from the Resource file :
<Label Content="{x:Static p:Resources.btn_tooltip_edit}"/>
This works fine.
To make my control styles reusable I pack them into an extra Resource Dictionary File.
The Problem is when i want to access the Resource File (.resx) from the Resource Dictionary (.xaml) its not possible and a XAMl Parse Exception (System.Windows.Markup.StaticExtension) is thrown.
<Setter Property="ToolTip" Value="{x:Static p:Resources.btn_tooltip_edit}"/>
Whats wrong?
Is it possible to access the Resource Files from a Style File?
Change the access modifier of Resource File from internal to public.
So I have an application and a class library.
The class library has
UserControls/Test.xaml
Images/TestImage.png
Test image is marked as 'Content' and 'Copy to Output Directory' is set to false because I want all of my images to be stored inside the class dll.
Test.xaml looks something like this...
<UserControl x:Class="TestLib.UserControls.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
>
<Image Source="/TestLib;component/Images/TestImage.png"/>
</UserControl>
The designer works fine but when I try to instantiate the UserControl/Test.xaml in my application then it throws the following exception:
System.IO.IOException occurred
Message=Cannot locate resource 'images/TestImage.png'.
Source=PresentationFramework
StackTrace:
at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
InnerException:
I have tried changing the resource type to "Resource" and "Embedded Resource" but neither makes any difference. I have also tried using the full pack string
"pack://application,,,/TestLib;component/Images/TestImage.png"
but it throws the exact same exception even stating the same 'images/TestImage.png'.
Edit: I forgot to mention that Images/TestImage.png is not "included" it is "included as link". Apparently this is the problem because of a compiler bug: Linked Files within a Folder Structure
The last URI should work with 'Resource' build action. Try code from this answer.
It looks like the solution is to not use "linked" instead of "included" resources within libraries. The compiler fails to respect their path which makes them impossible to reference when using them inside of a class library. See: Linked Files within a Folder Structure
Even if the linked file lives on the root of a project you still cannot reference it at runtime.
I'm, a WPF & Xaml-noob, for console applications it has worked for me to simply give the relative path to the executable to access a file.
It doesn't seem to work in xaml for me. (code at the bottom)
Absolute path works perfectly.
Is it possible in XAML in a WPF-application to access a file by simply using a relative path to the directory of your executable as a valid UriSource? If yes how and if not why not?
I found the following question, where they were talking about adding the file via Visual Studio's "Add existing item", so it seems like a different issue.
How can I set the WPF BitmapImage UriSource property to a relative path?
<Window.Icon>
<!--absolute path works:-->
<BitmapImage UriSource="C:\LongPath\SolutionFolder\ProjectFolder\bin\Debug\path4.ico" />
<!--none of the following relative paths worked:-->
<!--AppDomain.CurrentDomain.BaseDirectory returns the Debug-folder-->
<!--<BitmapImage UriSource="path4.ico" />-->
<!--<BitmapImage UriSource="../path4.ico" />-->
<!--<BitmapImage UriSource="Debug/path4.ico" />-->
<!--<BitmapImage UriSource="bin/Debug/path4.ico" />-->
<!--<BitmapImage UriSource="../bin/Debug/path4.ico" />-->
<!--<BitmapImage UriSource="../../bin/Debug/path4.ico" />-->
<!--<BitmapImage UriSource="../Debug/path4.ico" />-->
</Window.Icon>
URIs can be confusing, as they can refer to both files on disk and resources in the application. So a relative path could be to a resource in the app, when you intend it to be on disk.
You can use the siteoforigin authority to force relative file URIs. This blog explains this more, but here an example:
pack://siteoforigin:,,,/path4.ico
Relative paths without qualifications look up the referenced file in the application resources, if a path should be relative to the executable you can use pack://siteoforigin:,,,/path4.ico, see Pack URIs on MSDN.
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"/>
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!