首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >任务中的WritePixels of WriteableBitmap

任务中的WritePixels of WriteableBitmap
EN

Stack Overflow用户
提问于 2013-08-08 20:55:28
回答 2查看 1.1K关注 0票数 1

一个小小的n00b问题,我仍然不懂阅读,很多StackOverflow的答案。

colorData变量是由Kinect更新25次/s的字节数组。没有UI组件。

我以为WriteableBitmap和WritePixels在同一个任务的线程中被调用了。但我还是得到了System.InvalidOperationException。如果为每个循环创建一个新的WriteableBitmap,则没有错误。

应该如何修复我的代码以高效地重用我的WriteableBitmap?

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-14 20:51:45

因为WriteableBitmap被绑定到WPF呈现线程,所以我必须为进程间通信执行复杂的代码。

所以我不再使用它,而是使用Emgu CV (Open )中的图像,它也有更好的性能。

票数 0
EN

Stack Overflow用户

发布于 2013-09-13 06:27:54

Calling the WriteableBitmap.WritePixels method

检查高度和宽度的值。也许字节数组不够大!步幅是从内存中的一行像素到内存中的下一行像素的字节数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18135935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档