如何在单独的线程中复制和处理WriteableBitmap? [英] How to copy and process WriteableBitmap in separate thread?

查看:127
本文介绍了如何在单独的线程中复制和处理WriteableBitmap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的kinect应用程序有主线程,该线程可用于UI线程和其他线程之间的通信.我无法复制从kinect生成的WriteableBitmap,并将此WriteableBitmap图像通过EmguCV处理传递给单独的线程.我正在尝试所有操作:Clone,CloneCurrentValue,BlockingCollection,但是仍然存在一些问题,例如:

由于不同,调用线程无法访问该对象 线程拥有

或者处理数据是错误的. 这是我应用中的主循环;

WritableBitmap color; WritableBitmap depth; 
while (true) {          
    kinect.updateFrames();                
    ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
    {
       color = kinect.video.getBitmapColor();                   
       depth = kinect.video.getBitmapDepth();
    }));
    updateDetectors(color,depth); // Other thread
 }

解决方案

没有一个很好的,最小完整代码示例,即使不是不可能也不知道确切的问题是困难的,也不必介意如何解决.那说…

您可能已经知道,WriteableBitmap继承了DispatcherObject,并且只能在拥有它的调度程序线程中进行访问.

大概是真正创建对象的是对kinect.updateFrames()的调用,因此一个明显的解决方案是在被调用的匿名方法中而不是在其之前调用该方法. >

如果由于某些原因不可行,则可以选择冻结位图,然后再尝试在错误的线程中使用位图.例如:

kinect.updateFrames();                
color = kinect.video.getBitmapColor();
depth = kinect.video.getBitmapDepth();

color.Freeze();
depth.Freeze();

ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
{
    // use color and depth in other thread
}));

除非有任何限制,否则您可以直接访问位图的数据(例如CopyPixels()Lock()/BackBuffer),并使用该数据在正确的线程上创建新的位图.

如果上述方法都不对您有用,请按照上面提供的链接中的说明提供一个良好的代码示例.

I my kinect application I have main thread which is resposible for comunication between UI thread and other threads. I am no able to make a copy of WriteableBitmap generated from kinect and pass this WriteableBitmap image to separated thread with EmguCV processing. I was trying everything: Clone, CloneCurrentValue, BlockingCollection, but there are alwas some problems like:

The Calling thread cannot access this object because a different thread owns

Or processing data is wrong. This is main loop in my app;

WritableBitmap color; WritableBitmap depth; 
while (true) {          
    kinect.updateFrames();                
    ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
    {
       color = kinect.video.getBitmapColor();                   
       depth = kinect.video.getBitmapDepth();
    }));
    updateDetectors(color,depth); // Other thread
 }

解决方案

Without a good, minimal, complete code example that reliably reproduces the problem, it's difficult if not impossible to know what the exact problem is, never mind how to fix it. That said…

As you're probably aware, WriteableBitmap inherits DispatcherObject, and must be accessed only within the dispatcher thread that owns it.

Presumably, the call to kinect.updateFrames() is what actually creates the objects, and so one obvious solution would be to call that method in the invoked anonymous method instead of just before it.

If for some reason that's not feasible, an alternative would be to freeze the bitmaps before trying to use them in the wrong thread. E.g.:

kinect.updateFrames();                
color = kinect.video.getBitmapColor();
depth = kinect.video.getBitmapDepth();

color.Freeze();
depth.Freeze();

ctrlMainWindow.Dispatcher.BeginInvoke(new Action(() =>
{
    // use color and depth in other thread
}));

Barring any of that, you can access the bitmaps' data directly (e.g. CopyPixels() or Lock()/BackBuffer) and use that data to create new bitmaps on the correct thread.

If none of the above proves useful to you, please provide a good code example as described in the link provide above.

这篇关于如何在单独的线程中复制和处理WriteableBitmap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆