我不知道该怎么办!我正在尝试在后台线程中运行OpenCV并让它将图像发送给调度程序。这是代码的(蒸馏)版本:
private Thread _thread
private void WindowLoaded(){
_thread = new Thread(new ThreadStart(OpenCV)); //Run in seperate thread
_thread.Start();
}
private void OpenCV(){
//CV Image Capturing and parsing etc - works fine!
//...
//Problems begin
System.Drawing.Bitmap bm = BitmapConverter.ToBitmap(gray); //Get a usable image from CV
bm.SetResolution(640.0f, 480.0f); //Change its resolution
Dispatcher.BeginInvoke(new Action(() => { //Invoke this to be run in main thread
updateImage(bm); //Code that updates UI
}));
}
private void updateImage(System.Drawing.Bitmap img) {
ImageSourceConverter c = new ImageSourceConverter();
//Console.WriteLine(c == null); //No success in testing for nulls!
MyImage.Source = (ImageSource)c.ConvertFrom(img); //The problem is here
}
在updateImage
的最后一行标题中的错误发生了。我见过许多其他人遇到过非常相似的问题,但他们的解决方案似乎都没有用。
调用异常时,MyImage不为null,也不是c或img。但是,MyImage.Source是。