简体   繁体   English

如何在多线程WPF应用程序中显示更改的图像?

[英]How do I display changing images in a multithreaded WPF application?

I'm working on a project where we need to capture and process images in order to control a robot. 我正在开发一个项目,我们需要捕获和处理图像以控制机器人。 Those two things are working just fine. 这两件事情都很好。

What I can't seem to be able to figure out is how to use WPF to display the pre- and post-processing images. 我似乎无法弄清楚的是如何使用WPF来显示预处理和后处理图像。

I have an app that is using this code: 我有一个使用此代码的应用程序:

public CamNotification(string label1, string label2, BitmapSource image1, BitmapSource image2)

private bool hwLink_SetInfos(CamNotification info)
{
    try
    {
        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            lblText1.Content = info.Label1;
            lblText2.Content = info.Label2;

            ImageBox1.Source = info.Image1;
            ImageBox2.Source = info.Image2;
            InvalidateVisual();
        }));
        return true;
    }
    catch (Exception ex)
    {
        Debugger.Break();
        return false;
    }
}

It changes both the labels describing the images and the 'image.source's themselves. 它改变了描述图像的标签和'image.source'本身。

It works -> fine <- when I use a WPF-button to call the code, but when I try it using a WorkerThread from a Threadpool that replaces the two 'image.source's the images are not displayed, even though the labels are. 它工作 - > 很好 < - 当我使用WPF按钮来调用代码时,但是当我使用Threadpool中的WorkerThread来替换两个'image.source'时,即使标签是,也不会显示图像。

Moreover, if I have already pressed the button and the pictures were displayed, a call from the -> WorkerThread <- that ends up in the MainThread removes the images and leaves a blank area instead of updating them to the new content. 此外,如果我已按下按钮并显示图片,则来自 - > WorkerThread < - 的调用最终会在MainThread中删除图像并留下空白区域,而不是将其更新为新内容。

Using the Dispatcher I have already removed all crossthead-issues, Labels are updated but the "PresentationFramework -> System.Windows.Controls.Image" is cleard and not updated :-( 使用Dispatcher我已经删除了所有的crossthead问题,标签已更新,但“PresentationFramework - > System.Windows.Controls.Image”是清除的并且没有更新:-(

While looking through stackoverflow I found this question: 在查看stackoverflow时,我发现了这个问题:

WPF imaging problems WPF成像问题

It seems to be concerned with a problem similar to mine. 它似乎关注与我类似的问题。 However, neither the answers nor the OPs comment about how he solved his problem have helped me. 然而,答案和OP都没有评论他如何解决他的问题对我有所帮助。

I would appreciate any help you could give me. 我很感激你能给我的任何帮助。

BitmapImageSource is a freezable object which should be be frozen if you intend to access it across threads BitmapImageSource是一个可冻结的对象,如果您打算跨线程访问它,应该将其冻结

From MSDN 来自MSDN

A Freezable provides a Changed event to notify observers of any modifications to the object. Freezable提供Changed事件以通知观察者对对象的任何修改。 Freezing a Freezable can improve its performance, because it no longer needs to spend resources on change notifications. 冻结Freezable可以提高其性能,因为它不再需要在更改通知上花费资源。 A frozen Freezable can also be shared across threads, while an unfrozen Freezable cannot. 冻结的Freezable也可以在线程之间共享,而未冻结的Freezable则不能。

... ...

A freezable's Freeze method enables you to disable this self-updating ability. freezable的Freeze方法使您可以禁用此自我更新功能。 You can use this method to make the brush become "frozen," or unmodifiable. 您可以使用此方法使画笔变为“冻结”或不可修改。

As you're working with the images on another thread, you need to change your code to call Freeze() before it can be used on the UI thread. 当您在另一个线程上处理图像时,您需要更改代码以调用Freeze()然后才能在UI线程上使用它。 Some working code below 下面有一些工作代码

public CamNotification(string label1, string label2, BitmapSource image1, BitmapSource image2)

private bool hwLink_SetInfos(CamNotification info)
{
    try
    {
        info.Image1.Freeze();
        info.Image2.Freeze();

        Application.Current.Dispatcher.BeginInvoke((Action)(() =>
        {
            lblText1.Content = info.Label1;
            lblText2.Content = info.Label2;

            ImageBox1.Source = info.Image1;
            ImageBox2.Source = info.Image2;
            InvalidateVisual();
        }));
        return true;
    }
    catch (Exception ex)
    {
        Debugger.Break();
        return false;
    }
}

A couple of related articles: 几篇相关文章:

How do you pass a BitmapImage from a background thread to the UI thread in WPF? 如何将BitmapImage从后台线程传递到WPF中的UI线程?

In what scenarios does freezing WPF objects benefit performance greatly? 在什么情况下,冻结WPF对象会大大提高性能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在多线程应用程序中衡量负载平衡 - How do I measure load balancing in a multithreaded application 如何在Windows窗体应用程序中显示图像而又不知道需要显示的图像数量? - How do I display images in a Windows Forms Application without knowing the number of images that need to be displayed? 如何在具有更改页面的Core项目和WPF应用程序之间使用依赖注入? - How do I use Dependency Injection between a Core project and a WPF application with changing pages? 如何在 WPF 应用程序启动期间显示等待光标? - How do I display wait cursor during a WPF application's startup? 如何以编程方式退出 WPF 应用程序? - How do I exit a WPF application programmatically? 如何在Windows Phone 7应用程序中显示来自后台的图像? - How do I display images from back office in windows phone 7 application? 如何在WPF应用程序中循环浏览图像和视频? - How can I cycle through a mix of images and videos in a WPF application? 如何在多线程.NET应用程序中的单个表单上更新多个控件? - How do I update multiple controls on a single form in a multithreaded .NET application? 如何在WPF应用程序中显示视频 - How to display videos in WPF Application 如何在WPF应用程序中显示PowerPoint? - How to display a powerpoint in a wpf application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM