I had a coworker as me how to update an Image control in code. Specifically, he wanted to create a quick file browser that had a Next and Previous button to scroll through a preset list of Images.
Creating the list and iterating through directories is pretty easy, so I’ll omit that (Although, if someone wants to see that code, just comment on this post, and I’ll post it seperately).
Then, I just had a quick helper method that looks like this:
public void LoadImage(Image image, string imagePath)
image.Source = new BitmapImage(new Uri(imagePath));
The challenge that our designer ran into, is that Source requires an “ImageSource” class, and he didn’t know how to create one. BitmapImage is a class that inherits from ImageSource, and simply takes a Uri in the constructor with the location of the image. The helper function accepted the Image control as an argument, just so that I could use the same method to update any number of Image controls.
And really, that’s all there is to it. Of course, my version had a transparent InkCanvas on top of the Image, and I discussed how to load and save Ink in a previous post. If folks are interested, I can post more of the code for my Image/Markup Browser here.