如何在单独的线程中复制和处理WriteableBitmap?

编程入门 行业动态 更新时间:2024-10-19 22:43:01
本文介绍了如何在单独的线程中复制和处理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?

本文发布于:2023-10-16 16:31:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498108.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   如何在   WriteableBitmap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!