Home > DeveloperSection > Forums > How to Update/replace image in WFP by clicking button
Chintoo Semi

Total Post:135

Points:947
Posted on    September-21-2013 6:45 AM

 WPF WPF 
Ratings:


 1 Reply(s)
 843  View(s)
Rate this:

So I have an image that when the user clicks on a button it will change it to a new item. However, whenever the user clicks on one of the button, the window will go blank. How can I get this to work? Thank you.

private void Next_Click(object sender, RoutedEventArgs e)

{

    if (imageNumber > 6)

    {

        imageNumber = 1;

    }

    imageNumber++;

    string sUri = string.Format("@/Resources/{0}", imageSource[imageNumber]);

    Uri src = new Uri(sUri, UriKind.Relative);

    var bmp = new BitmapImage(src);

    img.Source = bmp;           

}

Xaml Code:-

<Image x:Name="img">

    <Image.Source>

        <BitmapImage UriSource="Resources/BlackJackTut-1.jpg" />

    </Image.Source>

</Image>



Sumit Kesarwani

Total Post:378

Points:2694
Posted on    September-21-2013 7:41 AM

Hi Chintoo,

In WPF application you can do same also with "pack://application:,,,/resources/imagename.png".

This way called Pack URI. This is static, but with these code you can do same an even use resource ;)

Put image in Resources.

private BitmapImage ConvertBitmapToBitmapImage(System.Drawing.Bitmap bitmap)

{

    MemoryStream memoryStream = new MemoryStream();

    bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);

    BitmapImage bitmapImage = new BitmapImage();

    bitmapImage.BeginInit();

    bitmapImage.StreamSource = new MemoryStream(memoryStream.ToArray());

    bitmapImage.EndInit();

    return bitmapImage;

}

and then use this:

private void btn_Click(object sender, RoutedEventArgs e)

{

    this.Img.Source = ConvertBitmapToBitmapImage(Properties.Resources.iamge1);

}


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

Follow MindStick