一个小小的n00b问题,我仍然不懂阅读,很多StackOverflow的答案。
colorData
变量是由Kinect更新25次/s的字节数组。没有UI组件。
我以为WriteableBitmap和WritePixels在同一个任务的线程中被调用了。但我还是得到了System.InvalidOperationException。如果为每个循环创建一个新的WriteableBitmap,则没有错误。
应该如何修复我的代码以高效地重用我的WriteableBitmap?
private async Task DoJob(TimeSpan dueTime, TimeSpan interval, CancellationToken token) {
if (dueTime > TimeSpan.Zero)
await Task.Delay(dueTime, token);
WriteableBitmap bitmap = NewColorBitmap(colorW, colorH);
// Repeat this loop until cancelled.
while (!token.IsCancellationRequested) {
try {
bitmap.WritePixels(
new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight),
colorData, bitmap.PixelWidth * Bgra32BytesPerPixel, 0);
}
catch(Exception ex){
// System.InvalidOperationException:
// The calling thread cannot access this object
// because a different thread owns it.
}
// Wait to repeat again.
if (interval > TimeSpan.Zero)
await Task.Delay(interval, token);
}
}
发布于 2013-08-14 20:51:45
因为WriteableBitmap被绑定到WPF呈现线程,所以我必须为进程间通信执行复杂的代码。
所以我不再使用它,而是使用Emgu CV (Open )中的图像,它也有更好的性能。
发布于 2013-09-13 06:27:54
Calling the WriteableBitmap.WritePixels method
检查高度和宽度的值。也许字节数组不够大!步幅是从内存中的一行像素到内存中的下一行像素的字节数。
https://stackoverflow.com/questions/18135935
复制相似问题