Emgu.cv WPF 绑定失败
Emgu.cv binding failure in WPF
我正在尝试从摄像头捕获视频并以 c# WPF 格式显示。但是当我启动程序时图像不显示。调试器也不例外,它只是 运行。我正在调试,我看到 CapturedImage 属性 正在按预期获取数据。可能是他们在不同的线程中工作。但我想不通。所以,帮帮我,
我正在绑定 ImageSource 类型 属性。如您所见,
public class VideoCapturing : INotifyPropertyChanged
{
private ImageSource capturedImage;
public ImageSource CapturedImage
{
get { return capturedImage; }
set { capturedImage = value; OnPropertyChanged("CapturedImage"); }
}
捕获代码也在这里,
public void run()
{
if (cap.capture == null)
{
capture = new Emgu.CV.VideoCapture(0);
CurrentFrame = new Mat();
}
capture.ImageGrabbed += VideoCapture_ImageGrabbed;
capture.Start();
}
private void VideoCapture_ImageGrabbed(object sender, EventArgs e) // runs in worker thread
{
capture.Retrieve(CurrentFrame);
CapturedImage = ImageSourceFromBitmap(CurrentFrame.ToImage<Emgu.CV.Structure.Bgr, byte>().ToBitmap());
}
} // VideoCapturing class ends.
这里是绑定的xaml部分,
<Grid>
<Image x:Name="img" Source="{Binding CapturedImage}"></Image>
</Grid>
这是mainwindow.xaml.cs,
public MainWindow()
{
InitializeComponent();
VideoCapturing VideoCapture = new VideoCapturing();
this.DataContext = VideoCapture ;
VideoCapture.run();
}
您调用OnPropertyChanged()
时有错别字。 "CaptureImage"
但应该是 "CapturedImage"
.
public ImageSource CapturedImage
{
get { return capturedImage; }
set { capturedImage = value; OnPropertyChanged("CapturedImage"); }
}
在 MainWindow
中你应该调用 VideoCapture.run()
而不是 capture.run()
。
因为 VideoCapture_ImageGrabbed
在工作线程中运行,您通过调用 Dispatcher.BeginInvoke
.
在 UI 线程上设置了 CapturedImage
private void VideoCapture_ImageGrabbed(object sender, EventArgs e)
{
capture.Retrieve(CurrentFrame);
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
CapturedImage = ImageSourceFromBitmap(CurrentFrame.ToImage<Emgu.CV.Structure.Bgr, byte>().ToBitmap());
}));
}
我正在尝试从摄像头捕获视频并以 c# WPF 格式显示。但是当我启动程序时图像不显示。调试器也不例外,它只是 运行。我正在调试,我看到 CapturedImage 属性 正在按预期获取数据。可能是他们在不同的线程中工作。但我想不通。所以,帮帮我,
我正在绑定 ImageSource 类型 属性。如您所见,
public class VideoCapturing : INotifyPropertyChanged
{
private ImageSource capturedImage;
public ImageSource CapturedImage
{
get { return capturedImage; }
set { capturedImage = value; OnPropertyChanged("CapturedImage"); }
}
捕获代码也在这里,
public void run()
{
if (cap.capture == null)
{
capture = new Emgu.CV.VideoCapture(0);
CurrentFrame = new Mat();
}
capture.ImageGrabbed += VideoCapture_ImageGrabbed;
capture.Start();
}
private void VideoCapture_ImageGrabbed(object sender, EventArgs e) // runs in worker thread
{
capture.Retrieve(CurrentFrame);
CapturedImage = ImageSourceFromBitmap(CurrentFrame.ToImage<Emgu.CV.Structure.Bgr, byte>().ToBitmap());
}
} // VideoCapturing class ends.
这里是绑定的xaml部分,
<Grid>
<Image x:Name="img" Source="{Binding CapturedImage}"></Image>
</Grid>
这是mainwindow.xaml.cs,
public MainWindow()
{
InitializeComponent();
VideoCapturing VideoCapture = new VideoCapturing();
this.DataContext = VideoCapture ;
VideoCapture.run();
}
您调用OnPropertyChanged()
时有错别字。 "CaptureImage"
但应该是 "CapturedImage"
.
public ImageSource CapturedImage
{
get { return capturedImage; }
set { capturedImage = value; OnPropertyChanged("CapturedImage"); }
}
在 MainWindow
中你应该调用 VideoCapture.run()
而不是 capture.run()
。
因为 VideoCapture_ImageGrabbed
在工作线程中运行,您通过调用 Dispatcher.BeginInvoke
.
CapturedImage
private void VideoCapture_ImageGrabbed(object sender, EventArgs e)
{
capture.Retrieve(CurrentFrame);
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
CapturedImage = ImageSourceFromBitmap(CurrentFrame.ToImage<Emgu.CV.Structure.Bgr, byte>().ToBitmap());
}));
}