loading pictures from a folder on loading time

落花浮王杯 提交于 2019-12-25 03:12:22

问题


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


回答1:


"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.



来源:https://stackoverflow.com/questions/4485969/loading-pictures-from-a-folder-on-loading-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!