90

I put an Image control on a Window and I would like to display an image that is stored in a project resource file named "Resources.resx". The name of the image in the resource file is 'Search'.

Could someone show me how I might go about doing this?

ASh
  • 34,632
  • 9
  • 60
  • 82
Hosea146
  • 7,412
  • 21
  • 60
  • 81

4 Answers4

125

If the image is in your resources folder and its build action is set to Resource. You can reference the image in XAML as follows:

"pack://application:,,,/Resources/Search.png"

Assuming you do not have any folder structure under the Resources folder and it is an application. For example I use:

ImageSource="pack://application:,,,/Resources/RibbonImages/CloseButton.png"

when I have a folder named RibbonImages under Resources folder.

wonea
  • 4,783
  • 17
  • 86
  • 139
Viv
  • 2,515
  • 2
  • 22
  • 26
  • What does the 'pack' namespace represent? – Hosea146 Apr 05 '11 at 17:45
  • 2
    here is info about pack uri: http://msdn.microsoft.com/en-us/library/aa970069(v=VS.90).aspx#The_Pack_URI_Scheme – Viv Apr 05 '11 at 17:48
  • 2
    Its been a while since I added images to my application... I might be worng here but I think you might need to build your application before it shows the image in WPF designer. – Viv Apr 05 '11 at 17:51
  • I'm trying to use a resource within the DLL in which it's defined. My images are under 'Resources/Images' and are marked as 'Resource'. However, nothing is showing up. Is there something different one needs to do for use in a DLL? – Mark A. Donohoe Jun 06 '16 at 19:19
  • 3
    I cant test it right now but from the MSDN link above, for referenced assemblies, it says use: "/ReferencedAssembly;component/Subfolder/ResourceFile.xaml". where "ReferencedAssembly" would be your assembly name. I've written code like: "/MyProgram.Namespace;component/Resources/imagename.png" – Viv Jun 06 '16 at 20:30
  • 2
    @wonea this work for me: "pack://application:,,,/YourAppName;component/Resources/FileDialog.png" with build action "Resource" – antonio Jan 16 '19 at 04:15
37

If you've got an image in the Icons folder of your project and its build action is "Resource", you can refer to it like this:

<Image Source="/Icons/play_small.png" />

That's the simplest way to do it. This is the only way I could figure doing it purely from the resource standpoint and no project files:

var resourceManager = new ResourceManager(typeof (Resources));
var bitmap = resourceManager.GetObject("Search") as System.Drawing.Bitmap;

var memoryStream = new MemoryStream();
bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Bmp);
memoryStream.Position = 0;

var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memoryStream;
bitmapImage.EndInit();

this.image1.Source = bitmapImage;
RandomEngy
  • 14,931
  • 5
  • 70
  • 113
  • 19
    There is a slight variation on this: `` Note the forward slash at the start which means look in the root folder. If the xaml file is not at the root, this is necessary as without the forward slash, it will start the search in the same directory as the xaml file. – Greg Gum Aug 12 '13 at 18:55
9

One of the benefit of using the resource file is accessing the resources by names, so the image can change, the image name can change, as long as the resource is kept up to date correct image will show up.

Here is a cleaner approach to accomplish this: Assuming Resources.resx is in 'UI.Images' namespace, add the namespace reference in your xaml like this:

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UI="clr-namespace:UI.Images" 

Set your Image source like this:

<Image Source={Binding {x:Static UI:Resources.Search}} /> where 'Search' is name of the resource.

Shrinand
  • 351
  • 3
  • 5
5
  1. Add folders to your project and add images to these through "Existing Item".
  2. XAML similar to this: <Image Source="MyRessourceDir\images\addButton.png"/>
  3. F6 (Build)
FearlessCoward
  • 327
  • 3
  • 6