使用后台线程生成的RenderTargetBitmap设置Image Source

时间:2015-01-22 15:07:47

标签: c# wpf multithreading

我在后台线程中应用模糊以提高性能。此函数返回RenderTargetBitmap。完成此操作后,我将通过Dispatcher调用图像上的更新并将其作为内容添加到页面中。这样做如下:

            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            Thread screenshotThread = new Thread(new ThreadStart(delegate()
            {
                RenderTargetBitmap img = CaptureScreen(0, 0, actualWidth, actualHeight);
                //System.Windows.Controls.Image image = imgBlur;
                //image = new System.Windows.Controls.Image();
                Application.Current.Dispatcher.Invoke(() =>
                {
                    image.Source = img;
                    image.Width = actualWidth;
                    image.Height = actualHeight;
                    PageContainer.Children.Add(image);
                });
            }));
            screenshotThread.SetApartmentState(ApartmentState.STA);
            screenshotThread.Start();

我将图像添加到PageContainer,这是一个网格。运行这段代码后,图像已添加到页面中。但是,imagesource为null ..当前没有可见的图像。如何显示此图像?

1 个答案:

答案 0 :(得分:2)

您已在不同的主题上创建了ImagecontrolRenderTargetBitmap。我很惊讶你没有得到例外。尝试添加img.Freeze();,然后再将其设置为Image.Source