使用 wpf 图像控件显示来自文件系统的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4332652/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:22:03  来源:igfitidea点击:

Use wpf image control to show an image from filesystem

wpfimagewpf-controls

提问by PVitt

I want to show an image from a file using an wpf image control. The image file resides in the application directory.

我想使用 wpf 图像控件显示来自文件的图像。图像文件驻留在应用程序目录中。

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

The file dashboard.jpgshould be replaceable during or after deployment. How do I have to add the image to the project and what BuildAction do I have to use to have the image read from the file system rather than any source I cannot change after deployment. What source uri do I have to use?

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

回答by Rudresh Bhatt

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

回答by battey

In markup:

在标记中:

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