Adding an image at runtime

I am doing apfication WPF with C #. I have three kinds of images in my Data folder. I have an Iamge abd text block and one button. when I click the button, it displays the text in the text block and depends on the text, the image may change. How can I add an image at runtime.

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

I know that it is incorrectly encoded. But I do not know what I can do for this. Img1.Source = ????????

+5
source share
1 answer

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>

C # button Press "Code":

 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);
    }
+2
source

All Articles