Set Image source from Resources in WPF application

守給你的承諾、 提交于 2019-12-20 10:56:17

问题


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

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

I want Image Source to be come from Resources.

Thanks.


回答1:


First you will have to include the image in your project with 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)

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

Element:

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


来源:https://stackoverflow.com/questions/15377431/set-image-source-from-resources-in-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!