在运行时添加图像

Anu*_*Anu 5 c# wpf image

我正在使用C#进行应用程序WPF.我的文件夹"数据"中有三种图像.我有Iamge abd文本块和一个按钮.当我按下按钮时,它将在文本块中显示文本并取决于文本,图像可能会有所不同.如何在运行时添加图像.

 public void Adddata(string lData)
        {          
            Text1.Text = lData; 
            Img1.Source = "data\vista_flag.png";
        }
Run Code Online (Sandbox Code Playgroud)

我知道我编码错了.但我不知道我能为此做些什么.Img1.Source = ????????

Pra*_*are 3

XAML:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Canvas Name="myCanvas">
    <StackPanel Name="stkPanel">
        <Button Name="btnLoadImage" Click="btnLoadImage_Click" >Load Image</Button>
    </StackPanel>
</Canvas>
Run Code Online (Sandbox Code Playgroud)

C#按钮点击代码:

 private void btnLoadImage_Click(object sender, RoutedEventArgs e)
    {
        string src = @"C:\Documents and Settings\pdeoghare\My Documents\My Pictures\YourImage.jpg";

        Image img = new Image();

        img.Source = new ImageSourceConverter().ConvertFromString(src) as ImageSource;

        stkPanel.Children.Add(img);
    }
Run Code Online (Sandbox Code Playgroud)