WPF 中的图像更新时发生 TargetInitationException

发布于 2024-08-18 16:15:56 字数 961 浏览 5 评论 0原文

我构建了一个显示图像的 WPF 控件。现在我想以非常快的速度改变这个形象。 我构建了一个 ImageContainer 类,它保存图像并有一个 ChangedEventHandler ,它在更改时更新我的​​控件中的图像。

执行的代码如下所示:

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();
            }
        }
    }

此代码抛出 System.Reflection.TargetInvocableException,我做错了什么?

I've built a WPF Control which displays an Image. 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-08-25 16:15:56

在我看来,您正在调用一个线程来调用一个线程?!

您是否尝试过直接调用调度程序上的操作,如下所示:

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;
                  }
              ));
}

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;
                  }
              ));
}
羅雙樹 2024-08-25 16:15:56

您是否尝试过简单地将 videoImage.Source 绑定到属性,并在 Instance_VideoRefresh 方法中更改该属性?

我之前已经尝试过使用 Image/List/Timer 组合,效果非常好。

Have you tried simply binding videoImage.Source to a property, and changing that property in your Instance_VideoRefresh method?

I've tried it before with an Image/List<ImageSource>/Timer combination, and it works pretty well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文