JimmyK
JimmyK

Reputation: 5002

Uri for Images Folder in WPF Applications

My project looks like this:

enter image description here

Ideally I would like to have all my images in the 'Images' folder, however at the moment, I have a bunch of images in Visual Studio 2013\Projects\TestProject\TestProject\bin\Debug\Images. I had to do this because as I was trying to find a way to change the source of a ImageBrush in code, I ended up doing this:

imagebrush.ImageSource = new BitmapImage(new Uri("Images/testimage.png", UriKind.Relative));

As much as I tried, I just couldn't find a way to point the Uri towards the Images folder in my first picture. Instead, I noticed the above code points towards the Debug folder of my project, hence why I ended up creating another 'Images' folder inside of there. In fact, I even found some tutorials that specifically say to do this when you want to change image sources in code.

This seems wrong however... Especially now that I essentially have two different Image folders. Can someone please explain to me how I can get my code to point towards the Images folder in my picture?

Upvotes: 5

Views: 9087

Answers (3)

Kumah Andrews
Kumah Andrews

Reputation: 51

For those who will later search for this Guest answer to

string imageLocation = @"..\..\Images\myImage.png";

Where Images is a folder in the project directory.

Upvotes: 0

Quest
Quest

Reputation: 135

To access the 'Images' folder from code behind type like this

     string imageLocation = @"\Images\myImage.png";

Upvotes: 1

JimmyK
JimmyK

Reputation: 5002

I always had trouble with this, but thanks to Ganesh I finally found a solution:

"pack://application:,,,/Images/testimage.png"

Pretty simple.

Upvotes: 5

Related Questions