Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageSource from a relative resource

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?

like image 862
00jt Avatar asked Sep 27 '12 22:09

00jt


3 Answers

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.

like image 72
Eric Olsson Avatar answered Oct 17 '22 20:10

Eric Olsson


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"/>
like image 43
N_A Avatar answered Oct 17 '22 20:10

N_A


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.

like image 41
J. Henry Slentz Avatar answered Oct 17 '22 21:10

J. Henry Slentz