【问题标题】:Updating bitmapSource - copying one to another更新 bitmapSource - 将一个复制到另一个
【发布时间】:2015-01-22 14:30:43
【问题描述】:

这是我的例外

The calling thread cannot access this object because a different thread owns it.

我的函数从计算中得到结果,我想更新一个已经打开的窗口..

 public override void UpdateResult(BaseMetricResults result)
        {
            var newResults = result as MetricUniformityResults;
            if (newResults == null)
            {
                return;
            }
            DispatcherHelper.UIDispatcher.Invoke(() =>
                {             
                    TopToBottomGraph.CrossSectionPoints.Clear();
                    foreach (var point in newResults.TopToBottomGraph.CrossSectionPoints)
                    {
                        TopToBottomGraph.CrossSectionPoints.Add(point);
                    }

                    newResults.JetMap.Freeze(); //exception here
                    byte[] arr = new byte[(int) (newResults.JetMap.Width*newResults.JetMap.Height*3)];
                    newResults.JetMap.CopyPixels(arr, (int) (newResults.JetMap.Width*3), 0);
                    JetMap = BitmapSource.Create((int) newResults.JetMap.Width, (int) newResults.JetMap.Height, 96, 96,
                                                 PixelFormats.Rgb24, BitmapPalettes.WebPalette, arr,
                                                 (int) (newResults.JetMap.Width*3));
                });
        }

这是我最后一次尝试我不确定是否必须冻结位图源...
无论如何 newResults.JetMap 是 BitmapSource,我有一个名为 JetMap 的属性,它是新的 BitmapSource,如何用新的图像更新旧图像?

【问题讨论】:

  • 异常究竟发生在哪里?哪种说法?
  • @Clemens newResults.JetMap.Freeze();
  • 如果您在 SO 中搜索您收到的错误消息,您将获得大约 400 个可能的答案,所有这些都提供了大致相同的指导:使用Dispatcher.Invoke()。如果您的场景需要不同的东西,您需要提供一个足够详细的问题,包括a complete code example 和清晰、准确的描述为什么它需要不同的东西。
  • @PeterDuniho 我认为你错了,我只是在滥用函数 freeze(),你可以看到它已经解决了。

标签: c# wpf bitmapsource


【解决方案1】:

您需要在创建后立即调用Jetmap.Freeze();,而不是在调度程序中,一旦它被冻结,您可以在调度程序中设置它并且您不会收到异常

【讨论】:

  • 试过了。仍然不知道执行会。移到行
  • 也许我应该切换到 writeablebitmap?还是在ui线程中分配bitmapsourceimage?
【解决方案2】:

您的 DispatcherHelper.UIDispatcher.Invoke 方法将在 UI 线程上执行。我最好的猜测是 newResults.JetMap 位图是在不同的线程上创建的,这会阻止您对其进行修改。同时,您不能创建要在 UI 线程以外的线程上显示的 JetMap 位图。因此,如果没有更多上下文,最好的建议是确保 newResults.JetMap 位图也在主 UI 线程中创建。

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多