我想将资源中的图像添加Image
到Button
.
UsingImage1.Source = Properties.Resources.MyImage
不起作用,因为Image1
is 是 typeImageSource
并且 Left side 是 type Drawing
。如何修复此代码以将我的资源中的图像显示为Image
按下按钮?
这似乎有效:
在 app.xaml 中:
<Application.Resources>
<Image x:Key="myImage" Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"/>
</Application.Resources>
在 MainWindow.xaml 中:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Grid x:Name="myGrid" Margin="5" Width="500">
<StackPanel>
<Button Content="Load Picture" Click="Button_Click_1"/>
<Image x:Name="ImageHolder"/>
</StackPanel>
</Grid>
在按钮单击事件中:
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Image img = Application.Current.Resources["myImage"] as Image;
ImageHolder.Source = img.Source;
}
所以在你的代码中你需要做:
Image myImage = Properties.Resources.MyImage as Image;
Image1.Source = myImage.Source;