Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 加载时从文件夹加载图片_Wpf_Image_Load - Fatal编程技术网

Wpf 加载时从文件夹加载图片

Wpf 加载时从文件夹加载图片,wpf,image,load,Wpf,Image,Load,您好,我正在尝试从我的程序开始时的文件中加载图像,原因不明 每当我以某种方式使用这些行时,我就会被从加载函数中抛出 当我在加载程序的过程中按下一个按钮时,它确实起作用,并且我能够加载图片 这是我的加载图片代码: Image pic = new Image(); string imagePath = String.Format(@"Images\{0}", 1); // this is ofc a file which is inside my debug pic.Source = new Bi

您好,我正在尝试从我的程序开始时的文件中加载图像,原因不明 每当我以某种方式使用这些行时,我就会被从加载函数中抛出 当我在加载程序的过程中按下一个按钮时,它确实起作用,并且我能够加载图片 这是我的加载图片代码:

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”的异常

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

提前感谢您的帮助

“图像\1”不是有效的URI。您可以使用FileInfo类创建Uri:

FileInfo fi = new FileInfo(imagePath);
Uri uri = new Uri(fi.FullName);
pic.Source = new BitmapImage(uri);

另外,还有一个技巧可以帮助您调试代码隐藏中的异常:打开“异常”窗口(ctrl+alt+e)并选中这两个框以查找公共语言运行时异常。这将导致错误发生时执行中断,从而更容易找出问题所在。

您的图像被称为“1”,没有任何扩展名?是的,我甚至尝试了1。jpg似乎是目录问题,我无法访问存在图像文件夹的调试中的数据目录