0

我想将资源中的图像添加ImageButton.

UsingImage1.Source = Properties.Resources.MyImage不起作用,因为Image1is 是 typeImageSource并且 Left side 是 type Drawing。如何修复此代码以将我的资源中的图像显示为Image按下按钮?

4

1 回答 1

0

这似乎有效:

在 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;
于 2013-09-17T07:53:58.793 回答