I have several images that i want to be Embedded into the exe.
When i set the Build Action to Embedded Resource I get through out the code an error that the Resource isn't available and asking me to set the Build Action to Resource
I Tried several different methods :
<ImageSource x:Key="Image_Background">YearBook;component/Resources/Images/darkaurora.png</ImageSource> <ImageSource x:Key="Image_Background">Images/darkaurora.png</ImageSource> <ImageSource x:Key="Image_Background">pack://application:,,,/Resources/Images/darkaurora.png</ImageSource>
This code sits in a Resource file. But none worked, they all throw this error :
Cannot convert the string 'pack://application:,,,/Resources/Images/darkaurora.png' into a 'System.Windows.Media.ImageSource' object. Cannot locate resource 'resources/images/darkaurora.png'. Error at object 'Image_Background' in markup file 'YearBook;component/Resources/ImageResources.xaml' Line 4 Position 6.
And in different places in code i get :
the file 'YearBook;component/Resources/Images/shadowdrop.png' is not a part of the project or its 'Build Action' property is not set to 'Resource'
So, What am i doing wrong?
Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.
Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string -to-object dictionary that maps a name to an object. The object may be a string , an image, an icon, or a blob of bytes.
To add a Resource Dictionary into your WPF application, right click the WPF project > add a Resource Dictionary. Now apply the resource "myAnotherBackgroundColor" to button background and observe the changes.
When you set the BuildAction to Resource it goes as embedded resource in an assembly. Or you can set BuildAction to Content then it will bundled into the resulting .xap file. You can use any one of these BuildActions. By setting BuildAction to Content you can access Image like: "/Resources/Images/darkaurora.png"
(must begin with slash). And when you use the BuildAction Resource then you can access image as "/YearBook;component/Resources/Images/darkaurora.png"
(assemblyname;component/relativepath). Hope this will help.
Just for those using xamarin forms and bump into this question, this can be done by creating a custom xaml markup extension explained here:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/images?tabs=windows
in the "Embedded Images"->"Using XAML" section
Citation of custom extension
[ContentProperty (nameof(Source))] public class ImageResourceExtension : IMarkupExtension { public string Source { get; set; } public object ProvideValue (IServiceProvider serviceProvider) { if (Source == null) { return null; } // Do your translation lookup here, using whatever method you require var imageSource = ImageSource.FromResource(Source, typeof(ImageResourceExtension).GetTypeInfo().Assembly); return imageSource; } }
citation of how to use
<?xml version="1.0" encoding="UTF-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:WorkingWithImages;assembly=WorkingWithImages" x:Class="WorkingWithImages.EmbeddedImagesXaml"> <StackLayout VerticalOptions="Center" HorizontalOptions="Center"> <!-- use a custom Markup Extension --> <Image Source="{local:ImageResource WorkingWithImages.beach.jpg}" /> </StackLayout> </ContentPage>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With