9

I'm trying to use a local resource to put an icon inside a Button. (C# Visual Studio 2012)

Directly inside my project i have a folder called "Resources" which contains "Icons/MyIcon.png"

The following code works (but is not using a relative path)

<Button Width="20" Height="20">
  <Button.Background>
        <ImageBrush ImageSource="C:\Documents\MyProject\Resources\Icons\MyIcon.png">
          </ImageBrush>
      </Button.Background>

However, i want it to use a relative path... so i tried the following

  <ImageBrush ImageSource="..\MyProject\Resources\Icons\MyIcon.png">

It compiles, but then gives me the error:

{"Cannot locate resource 'myproject/resources/icons/myicon.png'."}

Then i found an article that talked about referencing the project and said to do:

"/[ assembly name ];component[ path within the project ]"

so i tried that:

<ImageBrush ImageSource="/MyProject;component\Resources\Icons\MyIcon.png">

But it doesn't work.

It DOES show up in the GUI-Builder... but when i actually try to "debug" the code... it fails to run.

If i change the path at all by adding '123' for example:

 <ImageBrush ImageSource="/MyProject;component\Resources\Icons\MyIcon123.png">

i get a pre-compile error: "cannot find the file specified". Once i remove the '123' the error goes away. And the icon again is displayed in the Form. But when i run... i still get:

{"Cannot locate resource 'resources/icons/myicon.png'."}

What is going on? Why can it only find the resource BEFORE it runs? and not when i actually start?

Gray
  • 27
  • 7
00jt
  • 2,818
  • 3
  • 25
  • 29
  • "*However, i want it to use a relative path... so i tried the following*": The thing to consider first is whether this file is declared as an embedded resource or an independent content. If this is a content then [this solution works](https://stackoverflow.com/questions/25835477/not-able-to-reference-image-source-with-relative-path-in-xaml/57411664#57411664). – mins Aug 08 '19 at 11:59

4 Answers4

9

Do you need to set the BuildAction of the image to Resource? Is it being built into the assembly? Or are you treating it like Content? In that case, you'd want to set the BuildAction to Content and set CopyToOutputDirectory appropriately.

Here's a link to an MSDN article treating Resources, Content files, etc.

Eric Olsson
  • 4,805
  • 32
  • 35
6

I know this is an old post but for VS2013 none of these examples worked for me but a combination of all of them is how I now have to reference my image resources & sources within a URI..

"pack://application:,,,/!APP NAME!;component/Resources/Purple.bmp"

Of course substitute !APP NAME! for yours.. "pack://application:,,,/test1;component/Resources/Purple.bmp"

Also substitute Resources/Purple.bmp with your path.

IMPORTANT: component/ is constant and is NOT part of the path.

KargWare
  • 1,746
  • 3
  • 22
  • 35
  • `pack` works for me. But `component/` is NOT part of the path, it is an **constant**. Only `Resources/Purple.bmp` is the path inside the other assembly. I need to find that with the [docu](https://learn.microsoft.com/en-us/dotnet/desktop/wpf/app-development/pack-uris-in-wpf?view=netframeworkdesktop-4.8) in detail. – KargWare Mar 07 '21 at 13:54
1

I covered this in a blog post here.

It basically looks like this for a folder named "Images". If your .xaml file is in a different folder then you need to do ../Images/favicon.ico. Notice that in neither case do you need to add the project name to the start of the path. If you need more details see the example project at the end of the post.

<Image Margin="5" Source="/Images/favicon.ico" Height="100"/>
N_A
  • 19,799
  • 4
  • 52
  • 98
  • Your method shows icon shows up in the Visual Studio Editor, but when i run it... it doesn't show up. I don't get an error... the button just has no icon. – 00jt Oct 01 '12 at 13:26
  • 1
    Can you post what you have? I don't see any places where you use `Source` – N_A Oct 01 '12 at 13:49
  • I found a solution. Thanks for the help – 00jt Oct 01 '12 at 14:19
  • @00jt i'm facing the same issue. Can you please share your solution. Thanks – Aakanksha Jul 17 '17 at 07:11
  • 1
    In the Solution Explorer, i had to select the Icon file: "Resources\Icons\MyIcon.png" Then under "properties" i had to change the "Build Action" to: "Resource" Then I was able to simply do: – 00jt Aug 01 '17 at 14:37
  • if the question was useful, maybe give it an up vote? Same with the accepted answer from Eric? – 00jt Aug 01 '17 at 14:53
0

This worked for me for relative path for an image whose resource folder is in the Application.

  <Image Source="..\Images\down_arrow.gif" />