WPF binding image from camera

836 Views Asked by At

Hello I'm new around here, used to be rather passive reader. I'm learning WPF + MVVM, and I think I'm missing something in the whole binding concept. I'm using flycapture2 SDK for point grey camera, according to attached examples I should call _ProgressChanged on image receive event and bind received image to the image.source property

private void m_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    BitmapSource image = (BitmapSource)e.UserState;
    this.Dispatcher.Invoke(DispatcherPriority.Render,
    (ThreadStart)delegate ()
    {
        image.Source = image;
    }
    );
}

But that doesn't look good to me, because we just overwrite the source image every time a new image arrives.

What I tried to do instead (following some online tutorials) was to bind the Image control source property through ViewModel property Images_s with the window code behind (setting DataContext to ViewModel) and then I would expect that everytime I change the viewModel.Images_s it should update the UI. Unfortunately that doesn't work and instead I got an empty window.

Also - Do I need to dispatch this task? As I imagine binding should update UI itself on the event of image variable change in the Window code behind (Or am I overestimating WPF super powers?) Thanks,

<Image Name="myImage" Source="{Binding Image_s}" Stretch="UniformToFill"/>

ViewModel:

public class ViewModel : INotifyPropertyChanged
{
private BitmapSource img;

public event PropertyChangedEventHandler PropertyChanged;

public void NotifyPropertyChanged(string propName)
{
    if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
}


public BitmapSource Image_s
{
    get { return this.img; }
    set
    {
        this.img = value;
        this.NotifyPropertyChanged("Image_s");

    }
}

Window code:

viewModel = new ViewModel();
this.DataContext = viewModel;

ViewModel:

    private void m_worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    BitmapSource image = (BitmapSource)e.UserState;
    this.Dispatcher.Invoke(DispatcherPriority.Render,
    (ThreadStart)delegate ()
    {
        viewModel.Image_s = image;
    }
    );
}
0

There are 0 best solutions below