2017-06-06 40 views
0

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

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#示例代码:

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 
+0

尝试设置OutputPixelFormat属性到由所述WPF的BitmapSource类支持的格式(见[的PixelFormats](https://msdn.microsoft.com/en-us/library/system.windows.media .pixelformats(v = vs.110)的.aspx))。然后直接调用[BitmapSource.Create()](https://msdn.microsoft.com/en-us/library/system.windows.media.imaging.bitmapsource.create(v = vs.110).aspx)。 – Clemens

回答

1

使用Dispatcher.BeginInvoke代替Dispatcher.Invoke(...设置图像异步。

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

它没有解决问题仍然延迟 –

+0

我知道,但为什么它仍然延迟UI像@Majid khalili说 –

+0

“* 2048 * 2000像素大小帧速率几乎50 fps *”意味着创建一个相当大的位图,将其作为BMP编码到MemoryStream中,并在20毫秒内从流中解码出BitmapSource。 – Clemens