Reputation: 389
I've noticed that when I add a line of <Window.Background></Window.Background>
in the XAML file or in the C# code this.Background = new ImageBrush(new BitmapImage(new uri([...]));
if I put in the "Uri" this: new Uri(@"pack://application:,,,/Myapp;component/image.jpg")
I get an error when I try to compile it which says: "Could not locate resource "image.jpg" "
But if I change this to new Uri(System.IO.Directory.GetCurrentDirectory()+"\\image.jpg");
it never gives me an error. Why???? What is the difference between both methods? I am very comfortable with Directory
but why the compiler doesn't show an error "Locating" the resource with IO.Directory
? What's the difference?
The questions may sound too noob, but I don't understand why it gives an error in @"pack://application[...]
and not with GetCurrentDirectory()
Upvotes: 1
Views: 740
Reputation: 4230
A pack://
Uri, will search embedded resources for the image. The resources are inside the DLL/EXE, rather than deployed to the same folder. To use an image as a WPF Resource you need to set the Build Action
on the image to Resource
.
Using the Directory
approach simply searches the physical file system for the image.
More info here on WPF Pack Uri's.
Upvotes: 2