从 WPF 应用程序中的资源设置图像源

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

Set Image source from Resources in WPF application

wpfimagewpf-controls

提问by Hardik

Can anyone show me how can I set source to image from Resource in XAML. Currently I have

谁能告诉我如何将源设置为 XAML 中的 Resource 中的图像。目前我有

 <Image x:Name="img" Panel.ZIndex="1" Source="D:\Img100.jpg"  Stretch="Uniform"/>

I want Image Source to be come from Resources.

我希望图像源来自资源。

Thanks.

谢谢。

回答by sa_ddam213

First you will have to include the image in your project with BuildActionResource

首先,您必须使用BuildAction在您的项目中包含图像Resource

Then you can use it directly in your Image

然后你可以直接在你的图像中使用它

 <Image Source="Img100.jpg"/>

Or make a reusable resource of the image

或者制作一个可复用的图片资源

App.xaml (or other resource dictionary)

App.xaml(或其他资源字典)

<Application.Resources>
    <BitmapImage x:Key="myImage" UriSource="Img100.jpg" />
</Application.Resources>

Element:

元素:

<Image Source="{StaticResource myImage}" />