Advertise with us

Adding Image in Run Time

Posted by  Ankit Singh
 1532  View(s)
Rate this:

 I want to add an image from my Resources to an Image on the press of a Button.

Using Image1.Source = Properties.Resources.MyImage Does not work because the Image1 is of type ImageSource and the Left side is of type Drawing. How can I fix this code to display the image in my resource to the Image on button press?

  1. Re: Adding Image in Run Time

    Hi ankit,

    This seems to work:

    In app.xaml:

        <Image x:Key="myImage" Source="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"/>

    in MainWindow.xaml:

    <Window x:Class="WpfApplication1.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="myGrid" Margin="5" Width="500">
            <Button Content="Load Picture" Click="Button_Click_1"/>
            <Image x:Name="ImageHolder"/>

    in button click event:

        private void Button_Click_1(object sender, RoutedEventArgs e)
            Image img = Application.Current.Resources["myImage"] as Image;
            ImageHolder.Source = img.Source;

    So in your code you need to do :

    Image myImage = Properties.Resources.MyImage as Image;

    Image1.Source = myImage.Source;

      Modified On Mar-31-2018 01:43:33 AM