C# 如何添加图像?

C# 如何添加图像?,c#,wpf,C#,Wpf,我想知道如何在c代码中的wpf应用程序中添加图像 我要添加此图像: <Image Name="imgRubanbleu" Source="Objet/rubanbleu.png" Height="19" Margin="34,252,354,231" RenderTransformOrigin="0.5,0.5" /> <Image Source="Images/terrain.png" Grid.Row="4" Grid.RowSpan="4" Grid.Column="0

我想知道如何在c代码中的wpf应用程序中添加图像

我要添加此图像:

<Image Name="imgRubanbleu" Source="Objet/rubanbleu.png" Height="19"
Margin="34,252,354,231" RenderTransformOrigin="0.5,0.5" />
<Image Source="Images/terrain.png" Grid.Row="4" Grid.RowSpan="4" Grid.Column="0" MouseUp="Image_MouseUp_1"/>
()

但它仍然不起作用


(对不起我的英语水平,我通常用法语工作)

在WPF中,我们倾向于使用XAML而不是过程代码。下面是一个
图像
的示例,当用户将鼠标移到图像上时,图像的
值发生了变化:

<Image Width="16" Height="16" Margin="3,0">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="Images\DefaultImage.png"/>
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Source" Value="Images\MouseOverImage.png"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>


现在我知道您希望在单击后进行更改,但这只是一个让您开始的示例。您可以从MSDN上的页面了解更多信息。您也可能希望查看MSDN上的页面。。。这使我们能够在
触发器
s中处理事件(例如您选择的
MouseUp
事件)。

在代码隐藏中,您需要编写完整的:

仅当图像文件是Visual Studio项目的一部分(位于名为
Objet
的子文件夹中)且其生成操作设置为
Resource
时,此选项才有效


也就是说,您的新图像控件
myImage
必须添加到您的UI中的某个位置。您似乎希望将其置于现有图像之上,因此应将其添加到同一容器中,例如:

<Grid x:Name="imageGrid">
    <Image ... MouseUp="Image_MouseUp_1"/>
</Grid>

你能更具体地说明什么是“不工作”吗?当我点击它时,什么也没有出现……你能检查一下你是否能很好地输入代码吗?那么你可能还需要设置你的“myImage”位置?我尝试了myImage。边距但仍然不起作用…我只想在单击第二个图像时将第一个图像叠加到第二个图像上。我如何看到他们的构建操作设置为Resource?
var uri = new Uri("pack://application:,,,/Objet/rubanbleu.png");

myImage.Source = new BitmapImage(uri); // BeginInit/EndInit not required!
<Grid x:Name="imageGrid">
    <Image ... MouseUp="Image_MouseUp_1"/>
</Grid>
private void Image_MouseUp_1(object sender, MouseButtonEventArgs e)
{
    var myImage = new Image();
    ...
    imageGrid.Children.Add(myImage);
}