Answers for "wpf bind image source to string"

C#
0

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));
    }
}
Posted by: Guest on February-03-2021

C# Answers by Framework

Browse Popular Code Answers by Language