The most suitable raster image class for the model

I am writing a simple WPF application using MVVM. What is the most convenient class for extracting raster images from models and further data binding: Bitmap, BitmapImage, BitmapSource?

public class Student { public <type?> Photo { get; } } 

Or maybe I can somehow convert Bitmap to BitmapSource using ViewModel?

+4
source share
2 answers

I believe a more flexible way is to return the photo (or any other bitmap) as a stream. In addition, if the photo was changed, the model must fire the event with the changed photo, and the client must process the event with the changed photo to get a new photo.

 public class PhotoChangedEventArgs : EventArgs { } public class Student { public Stream GetPhoto() { // Implementation. } public event EventHandler<PhotoChangedEventArgs> OnPhotoChanged; } public class StudentViewModel : ViewModelBase { // INPC has skipped for clarity. public Student Model { get; private set; } public BitmapSource Photo { get { BitmapImage image = new BitmapImage(); image.BeginInit(); image.StreamSource = Model.Photo; image.EndInit(); image.Freeze(); return image; } } public StudentViewModel(Student student) { Model = student; // Set event handler for OnPhotoChanged event. Model.OnPhotoChanged += HandlePhotoChange; } void HandlePhotoChange(object sender, PhotoChangedEventArgs e) { // Force data binding to refresh photo. RaisePropertyChanged("Photo"); } } 
0
source

I always use BitmapImage , it is rather specialized and offers nice properties and events that can be useful (for example, IsDownloading , DownloadProgress and DownloadCompleted ).

+1
source

All Articles