如何从文件位置WPF添加图像

我坚持在WPF中从我的文件位置加载图像。

这是我的xaml

 

这是我背后的代码

 internal int FindImages(string slugName, DirectoryInfo outputFolder) { if (slugName != null) { List filePathList = Directory.GetFiles(outputFolder.FullName).ToList(); List filePathList_ToBeDeleted = new List(); foreach (string filePath in filePathList) { if (Path.GetFileNameWithoutExtension(filePath).ToLower().Contains("_70x70")) { image1.Source = filePath; } } int count = 0; return count; } 

我的文件路径显示为"\\\\Server1\\Dev\\Online\\Images\\7PMa_Test3_0306_70x70.jpg"

这是捕获

 image1.Source = new BitmapImage(new Uri(filePath)); 

如果它是位于驱动器上某个位置的文件(不是资源),最好使用ABSOLUTE路径:

 image.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "image.png", UriKind.Absolute)); 

此代码检测正在运行的文件夹并构建相对于它的路径