在加载时间从文件夹加载图片 [英] loading pictures from a folder on loading time

查看:93
本文介绍了在加载时间从文件夹加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,由于某种未知原因,我正在尝试从程序的开头加载文件中的图像 每当我以某种方式使用这些行时,我就会被抛出加载函数 当我不在程序加载期间按下按钮时,它确实可以工作并且我能够加载图片 这是我正在加载的图片代码:

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

更多信息:当我试图将此行放入构造函数时,由于某种原因,我得到了一个异常: PresentationFramework.dll中出现了"System.Windows.Markup.XamlParseException"类型的第一次机会异常

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

其他信息:与指定的绑定约束匹配的对类型为"yad2.PresentationLayer.MainWindow"的构造函数的调用引发了异常.行号"5"和行位置"9".

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

在此先感谢您的帮助

推荐答案

"Images \ 1"不是有效的URI.您可以使用FileInfo类创建Uri:

"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);

此外,还有一条提示可帮助您在隐藏代码中调试异常:打开异常"窗口(ctrl + alt + e)并选中公共语言运行时异常"两个框.当发生错误时,这将导致执行中断,从而更容易找出问题所在.

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.

这篇关于在加载时间从文件夹加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆