bircastri
bircastri

Reputation: 2169

How to get Image from resource project

I'have this package on my project:

ProjectName : Gestore

---View (Folder)

------Resources (Folder)

---------Images (Folder)

------------immagine1.png

---DatiAnagraficiPageView.xaml

Now I would like to create programmatically an Image, so I try to write this code but it not found. THe code is:

System.Windows.Controls.Image img = new System.Windows.Controls.Image();
img.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory() + @"\Resources\Images\immagine1.png"));

I have an exception because the control not find a image.

Can we help me?

I think the error is in the code "Directory.GetCurrentDirectory()"

Upvotes: 1

Views: 84

Answers (3)

n32303
n32303

Reputation: 829

try

System.Windows.Controls.Image img = new System.Windows.Controls.Image();
BitmapImage bm = new BitmapImage(new Uri("@View/Resources/Images/immagine1.png", UriKind.RelativeOrAbsolute));
            img.Source = bm;

Upvotes: 0

samwise
samwise

Reputation: 2267

Directory.GetCurrentDirectory() doesn't give you the path where you are calling from but where your executable is executed from.

http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory(v=vs.110).aspx

Upvotes: 1

dkozl
dkozl

Reputation: 33384

Assuming that immagine1.png is a resource you should be using Pack URI, something like

new Uri("pack://application:,,,/Gestore;component/View/Resources/Images/immagine1.png")

Upvotes: 3

Related Questions