简体   繁体   English

WPF中的图像更新的TargetInvocationException

[英]TargetInvocationException on Image update in WPF

I've built a WPF Control which displays an Image. 我已经构建了一个显示图像的WPF控件。 Now I would like to change that image at a very fast rate. 现在我想以非常快的速度更改该图像。 I've build an ImageContainer class which holds the image and has a ChangedEventHandler which updates the Image in my control when changed. 我已经构建了一个ImageContainer类来保存图像,并且有一个ChangedEventHandler,可以在更改时更新控件中的Image。

The code which is executed looks like this: 执行的代码如下所示:

videoImageThread = new Thread(
            new ThreadStart(
              delegate()
              {
                  this.VideoCapture.Dispatcher.Invoke(
                    System.Windows.Threading.DispatcherPriority.Normal,
                    new Action(
                      delegate()
                      {

                          videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;

                      }
                  ));
              }
          ));


private void Instance_VideoRefresh()
    {
        if (VideoImageContainer.Instance.VideoImage != null)
        {
            lock (videoImageSetLock)
            {
                videoImageThread.Start();
            }
        }
    }

This code throws a System.Reflection.TargetInvocationException, what am I doing wrong? 这段代码抛出一个System.Reflection.TargetInvocationException,我做错了什么?

seems to me like you are invoking a thread to invoke a thread ?! 在我看来,你正在调用一个线程来调用一个线程?

have you tried invoking the action on the dispatcher directly like so: 您是否尝试过直接调度调度程序上的操作,如下所示:

private void Instance_VideoRefresh()
{
    if (VideoImageContainer.Instance.VideoImage != null)
        this.VideoCapture.Dispatcher.Invoke(
                System.Windows.Threading.DispatcherPriority.Normal,
                new Action(
                  delegate()
                  {
                      videoImage.Source = VideoImageContainer.Instance.VideoBitmapSourceImage;
                  }
              ));
}

Have you tried simply binding videoImage.Source to a property, and changing that property in your Instance_VideoRefresh method? 您是否尝试过将videoImage.Source绑定到属性,并在Instance_VideoRefresh方法中更改该属性?

I've tried it before with an Image/List<ImageSource>/Timer combination, and it works pretty well. 我之前尝试过使用Image / List <ImageSource> / Timer组合,它运行得很好。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM