Reputation: 2717
hello i am trying to load images from files in the start of my program and for some unknown reason whenever i use those lines somehow i am getting thrown out of my loading function when i press on a button not during the load of the program it does work and i am able to load pictures this is my loading pictures code :
Image pic = new Image();
string imagePath = String.Format(@"Images\{0}", 1); // this is ofc a file which is inside my debug
pic.Source = new BitmapImage(new Uri(imagePath)); // folder
more info: when i am trying to put this line in my constructor i am getting for some reason an exception: A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Additional information: 'The invocation of the constructor on type 'yad2.PresentationLayer.MainWindow' that matches the specified binding constraints threw an exception.' Line number '5' and line position '9'.
thanks in advance for your help
Upvotes: 0
Views: 931
Reputation: 20840
"Images\1" is not a valid URI. You can create the Uri by using the FileInfo class:
FileInfo fi = new FileInfo(imagePath);
Uri uri = new Uri(fi.FullName);
pic.Source = new BitmapImage(uri);
Also, a tip to help you debug exceptions in code-behind: Open the Exceptions window (ctrl+alt+e) and check both boxes for Common Language Runtime Exceptions. This will cause execution to break when the error occurs, making it a lot easier to work out what the problem is.
Upvotes: 1