32

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?

eric.itzhak
  • 15,752
  • 26
  • 89
  • 142

3 Answers3

33

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.

Dov
  • 15,530
  • 13
  • 76
  • 177
yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • 25
    "Embedded Resource" and "Resource" are different. If you look at the generated assembly in Reflector or ILSpy you will see that they get included in different ways. The author asked how to do it with "Embedded Resource", not "Resource". – BrainSlugs83 Nov 01 '13 at 23:19
  • @ethicallogics but what if need to bind this resource via viewmodel? Is there any other option, than bind to string path? – nuclear sweet Aug 21 '15 at 10:17
  • 6
    You need a leading slash on the assemblyname when BuildAction is Resource: "/YearBook;component....". See http://stackoverflow.com/questions/347614/wpf-image-resources –  Feb 01 '16 at 19:55
  • 2
    I think it should be "pack://application:,,,/Resources/Images/darkaurora.png" in the case of Resource as a BuildAction – Ahm3d Said May 06 '16 at 08:26
  • 1
    Since this question has been tagged as WPF, this question about Resource vs. Embedded Resource seems relevant: https://stackoverflow.com/questions/1934826 – HotN Jan 07 '19 at 17:59
1

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://learn.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>
t.shikanai
  • 71
  • 1
  • 1
-1

ImageSource cannot be instantiated.

public abstract class ImageSource : Animatable, 
IFormattable

There's that little abstract in there which will screw your day up. Your xaml is actually trying to instantiate an instance of ImageSource, then assign the value within the element (your Uri, in this case) to a property marked with the ContentPropertyAttribute (??) using whatever converter that could be located to convert the string to an object (again, ??).

I think you want a BitmapSource.

<BitmapImage 
    x:Key="Image_Background" 
    UriSource="/Images/darkaurora.png" />
  • 4
    Actually you got this one wrong, you can declare `ImageSources` in XAML just fine because the class has a [type converter](http://msdn.microsoft.com/en-us/library/cc645047.aspx) associated with it (see the attributes in [the docs](http://msdn.microsoft.com/en-us/library/system.windows.media.imagesource.aspx)). Can't tell you where that is documented but i think you can trust me on that. You get that error only because the inner-XML of the tag is missing. – H.B. Feb 24 '12 at 00:25
  • @H.B.: Ouch. You're right. It converts a string into a BitmapFrame (assuming the string is a valid Uri pointing to an image file somewhere). –  Feb 24 '12 at 14:21