i am using in my view in an Image Control to display my webcam
in the view’s cs file i used EmguCV with a background worker to sample each frame and inject it to the Image.source and it worked perfectly.
Now i’m trying to do it with binding via the ViewModel but the Image.Source never updates.
View:
<Image x:Name="imgVideo" Canvas.Left="0" Canvas.Top="0" Stretch="Fill" Source="{Binding Path=ImageSource}" Width="400" Height="266" />
ViewModel (relevant parts):
using System.Windows.Media;
ImageSource imageSource;
public ImageSource ImageSource
{
get { return imageSource; }
set
{
imageSource = value;
this.RaisePropertyChanged("ImageSource");
}
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
Image<Bgr, Byte> currentFrame = m_capture.QueryFrame();
...
e.Result = currentFrame ;
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
Image<Bgr, Byte> result = e.Result as Image<Bgr, Byte>;
BitmapSource bitmapSource = ToBitmapSource(result);
JpegBitmapEncoder encoder = new JpegBitmapEncoder();
MemoryStream memoryStream = new MemoryStream();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);
var source = new BitmapImage();
source.BeginInit();
source.StreamSource = new MemoryStream(memoryStream.ToArray());
source.EndInit();
memoryStream.Close();
ImageSource = source;
result.Dispose();
}
any help will be highly appreciated, Thanks.
I presume (even it’s hard to say by just reading this post) that there is no any
DataContextassigned to<Image.... />. That is, imo, most possible reason of your problem.