Home > DeveloperSection > Forums > Adding Image in Run Time
Ankit Singh

Total Post:341

Posted on    September-23-2013 8:14 AM


 1 Reply(s)
 867  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?

Pravesh Singh

Total Post:412

Posted on    September-23-2013 8:39 AM

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;

Don't want to miss updates? Please click the below button!

Follow MindStick