C# 如何从文件位置添加图像 WPF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16888890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add images from file location WPF
提问by Usher
Am stuck to load images from my file location in WPF.
我坚持从我在 WPF 中的文件位置加载图像。
here is my xaml
这是我的 xaml
<Image Grid.ColumnSpan="3" Grid.Row="11" Height="14" HorizontalAlignment="Left" Margin="57,1,0,0" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="108" />
Here is my code behind
这是我的代码
internal int FindImages(string slugName, DirectoryInfo outputFolder)
{
if (slugName != null)
{
List<string> filePathList = Directory.GetFiles(outputFolder.FullName).ToList();
List<string> filePathList_ToBeDeleted = new List<string>();
foreach (string filePath in filePathList)
{
if (Path.GetFileNameWithoutExtension(filePath).ToLower().Contains("_70x70"))
{
image1.Source = filePath;
}
}
int count = 0;
return count;
}
My file path shows like "\\\\Server1\\Dev\\Online\\Images\\7PMa_Test3_0306_70x70.jpg"
我的文件路径显示为 "\\\\Server1\\Dev\\Online\\Images\\7PMa_Test3_0306_70x70.jpg"
采纳答案by Nikhil Agrawal
Here's the catch
这是问题
image1.Source = new BitmapImage(new Uri(filePath));
回答by user1780040
If it's a file located somewhere on the drive (not a resource), better use an ABSOLUTE path:
如果它是位于驱动器上某处的文件(不是资源),最好使用绝对路径:
image.Source = new BitmapImage(new Uri(AppDomain.CurrentDomain.BaseDirectory + "image.png", UriKind.Absolute));
This code detects the running folder and builds the path relative to it
此代码检测正在运行的文件夹并构建相对于它的路径

