更新 bitmapSource - 将一个复制到另一个

Updating bitmapSource - copying one to another

这是我的例外

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

我的函数从计算中获取结果,我想更新已经打开的 window..

 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,我怎样才能用新图像更新旧图像?

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

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