使用WPF图像控件显示文件系统中的图像

22

我想通过 WPF 图像控件从文件中显示一张图片,该图片文件位于应用程序目录中。

<Image Stretch="Fill" Source="dashboard.jpg" />

在或者部署期间应该可以替换文件dashboard.jpg。我该如何将图像添加到项目中,以及我需要使用什么BuildAction才能从文件系统中读取图像,而不是在部署后无法更改的任何源。我需要使用什么源URI?

2个回答

35
ImageSource imageSource = new BitmapImage(new Uri("C:\\FileName.gif"));    
image1.Source = imageSource;

24

在标记中:

<Image Stretch="Fill">
    <Image.Source>
        <BitmapImage UriSource="dashboard.jpg"/>
    </Image.Source>
</Image>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接