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

问题描述 投票:12回答:2

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

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

我希望图像源来自资源。

谢谢。

wpf image wpf-controls
2个回答
28
投票

首先,您必须将图像包含在BuildAction Resource

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

 <Image Source="Img100.jpg"/>

或制作图像的可重用资源

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

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

元素:

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

0
投票

为了直接从XAML访问,请在资源文件中添加图像(“ Img100.jpg”)。

现在在XAML标头中添加项目属性的引用:

xmlns:properties="clr-namespace:MyProjectName.Properties"

现在像下面一样访问图像:

<image source="{Binding Source={x:Static properties:Resources.Img100}}" />
© www.soinside.com 2019 - 2024. All rights reserved.