WPF中如何高效刷新多画面

通过ffmpeg解码并渲染到image控件,在渲染多个image时,画面明显变慢,cpu占用也高,有没有别的渲染方案

我试过将DispatcherPriority.Background改成DispatcherPriority.Normal,画面明显变流畅了,但其他交互变得很卡(例如点击别的按钮时,很迟才响应);猜想可能是wpf只有一条ui线程,同时刷新这么多个image画面导致的卡顿。

try
            {
                while (flg)
                {
                    if (d == null) break;
                    this.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
                    {
                        try
                        {
                            ImageSource src = ChangeBitmapToImageSource(new Bitmap(d.avFrame.width, d.avFrame.height, d.avFrame.linesize[0], System.Drawing.Imaging.PixelFormat.Format24bppRgb, (IntPtr)d.avFrame.data[0]));
                            if (src != null)
                            {
                                d.image.Source = src;
                            }
                        }
                        catch
                        {
                            flg = false;
                        }
                    }));
                    Thread.Sleep(10);
                }
            }

直接找开源的代码参考就行

https://github.com/search?q=WPF+ffmpeg

用backgroundworker 试试,不涉及修改UI的部分全写在 backgroundworker 里面