2009-08-05 70 views
1

我有一個特殊的攝像機(使用GigEVision協議),我使用提供的庫進行控制。我可以訂閱幀接收事件,然後通過IntPtr訪問幀數據。如何從WPF中的原始幀渲染視頻?

在我以前的WinForms應用程序中,我可以通過從數據創建一個Bitmap對象並將其設置爲PictureBox圖像,或者通過將PictureBox句柄傳遞給提供的庫中的一個函數來渲染該框架,該函數將直接在區。

在WPF中做類似的事情的最好和最快的方法是什麼?攝像機的運行速度從30到100 fps。

編輯(1):

由於框架接收到的事件是未在UI線程它具有跨線程工作。

編輯(2):

我發現使用WriteableBitmap的一個解決方案:

void camera_FrameReceived(IntPtr info, IntPtr frame) 
{ 
    if (VideoImageControlToUpdate == null) 
    { 
     throw new NullReferenceException("VideoImageControlToUpdate must be set before frames can be processed"); 
    } 

    int width, height, size; 
    unsafe 
    { 
     BITMAPINFOHEADER* b = (BITMAPINFOHEADER*)info; 

     width = b->biWidth; 
     height = b->biHeight; 
     size = (int)b->biSizeImage; 
    } 
    if (height < 0) height = -height; 

     //Warp space-time 
     VideoImageControlToUpdate.Dispatcher.Invoke((Action)delegate { 
     try 
     { 
      if (VideoImageControlToUpdateSource == null) 
      { 
       VideoImageControlToUpdateSource = 
        new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, BitmapPalettes.Gray256); 
      } 
      else if (VideoImageControlToUpdateSource.PixelHeight != height || 
        VideoImageControlToUpdateSource.PixelWidth != width) 
      { 
       VideoImageControlToUpdateSource = 
        new WriteableBitmap(width, height, 96, 96, PixelFormats.Gray8, BitmapPalettes.Gray256); 
      } 

      VideoImageControlToUpdateSource.Lock(); 

      VideoImageControlToUpdateSource.WritePixels(
       new Int32Rect(0, 0, width, height), 
       frame, 
       size, 
       width); 

      VideoImageControlToUpdateSource.AddDirtyRect(new System.Windows.Int32Rect(0, 0, width, height)); 
      VideoImageControlToUpdateSource.Unlock(); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
     } 
    }); 
} 

在上文中,VideoImageControlToUpdate是一個WPF圖像控制。

對於更多的速度,我相信codeplex上的VideoRendererElement更快。

回答

0

最好的辦法: WriteableBitmap.WritePixels(...,IntPtr的來源,...)

的最快方法:使用 WIC和IntPtr的非託管內存中的所有操作。但爲什麼在這種情況下使用WPF呢?如果需要這種性能,請考慮使用DirectX覆蓋。

+0

您是否有鏈接到使用DirectX的示例? – 2009-09-27 16:35:25