C# WPF中的大型图像更新延迟以进行实时监控

C# WPF中的大型图像更新延迟以进行实时监控,c#,wpf,bitmap,type-conversion,imagesource,C#,Wpf,Bitmap,Type Conversion,Imagesource,我正在从USB获取图像序列,每次抓取图像后,我将抓取的结果转换为System.Drawing.Bitmap,然后将其转换为System.Windows.Mesia.Imging.BitmapImage,以便能够将其分配给Imagesource,并最终在dispatcher线程中更新UI,所有这些过程都需要时间,而且不会启动,摄像机公司(Basler)的示例代码使用C#并直接将System.Drawing.Bitmap分配给图片框,可以毫不延迟地显示实时视图。 处理它的最佳解决方案是什么?值得一提

我正在从USB获取图像序列,每次抓取图像后,我将抓取的结果转换为System.Drawing.Bitmap,然后将其转换为System.Windows.Mesia.Imging.BitmapImage,以便能够将其分配给Imagesource,并最终在dispatcher线程中更新UI,所有这些过程都需要时间,而且不会启动,摄像机公司(Basler)的示例代码使用C#并直接将System.Drawing.Bitmap分配给图片框,可以毫不延迟地显示实时视图。 处理它的最佳解决方案是什么?值得一提的是,2048*2000像素的帧速率几乎为50 fps

PixelDataConverter converter = new PixelDataConverter();
            Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
            BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            converter.OutputPixelFormat = PixelType.BGRA8packed;
            IntPtr ptrBmp = bmpData.Scan0;
            converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); 
            bitmap.UnlockBits(bmpData);

            BitmapImage bitmapimage = new BitmapImage();
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Bmp);
                memory.Position = 0;
                bitmapimage.BeginInit();
                bitmapimage.StreamSource = memory;
                bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapimage.EndInit();
                bitmapimage.Freeze();
            }
            Dispatcher.Invoke(new Action(() =>
            {
                imgMain.Source = bitmapimage;
            }));
这是c#by company的示例代码:

Bitmap bitmap = new Bitmap(grabResult.Width, grabResult.Height, PixelFormat.Format32bppRgb);
                    // Lock the bits of the bitmap.
                    BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
                    // Place the pointer to the buffer of the bitmap.
                    converter.OutputPixelFormat = PixelType.BGRA8packed;
                    IntPtr ptrBmp = bmpData.Scan0;
                    converter.Convert(ptrBmp, bmpData.Stride * bitmap.Height, grabResult); //Exception handling TODO
                    bitmap.UnlockBits(bmpData);

                    // Assign a temporary variable to dispose the bitmap after assigning the new bitmap to the display control.
                    Bitmap bitmapOld = pictureBox.Image as Bitmap;
                    // Provide the display control with the new bitmap. This action automatically updates the display.
                    pictureBox.Image = bitmap;
                    if (bitmapOld != null)
                    {
                        // Dispose the bitmap.
                        bitmapOld.Dispose();
                    }
                }
enter code here
使用而不是
Dispatcher.Invoke(…
将映像设置为异步

       Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
       {
             imgMain.Source = bitmapimage;
       }));

它没有解决仍然延迟的问题我知道,但是为什么它仍然延迟用户界面,就像@Majid khalili说的“2048*2000像素大小,帧速率几乎是50 fps”意味着创建一个相当大的位图,将其编码为BMP到MemoryStream中,并在20毫秒内从流中解码BitmapSource。是的,但公司的示例代码正在运行,我将用c#中的公司示例cod更新问题,尝试将OutputPixelFormat属性设置为WPF BitmapSource clas支持的格式然后直接打电话。