wpf bind image source to string code example

Example: wpf bind image source to string

using Prism.Commands;
using System.IO;
using System.Windows.Media;
using System.Windows.Media.Imaging;

public class ImageViewer : BindableBase
{
	// Bind this in your Xaml file like this:
    // <Image Source="{Binding ImageSource}" />
    public ImageSource ImageSource { get; private set; }
        
    public DelegateCommand LoadPhotoCommand { get; private set; }
        
    public ImageViewer()
    {
        LoadPhotoCommand = new DelegateCommand(LoadPhoto);
    }

    private void LoadPhoto()
    {
        byte[] data = File.ReadAllBytes(@"E:\Images\Image.jpg");

        var source = new BitmapImage();
        source.BeginInit();
        source.StreamSource = new MemoryStream(data);
        source.EndInit();

        ImageSource = source;
        RaisePropertyChanged(nameof(ImageSource));
    }
}

Tags:

Misc Example