2013-06-13 69 views
0

我正在使用WPF顯示Kinect ColorImageFrame和骨架表示的項目。我還必須錄製這兩個視頻。C#MemoryStream減慢程序性能

我能夠顯示和記錄(使用EmguCV)這兩個圖像,但我有一些性能問題。看起來我的代碼的這部分是我失去了性能的原因。

private void DrawSkeleton(Skeleton[] skeletons) 
    { 
     using (System.Drawing.Bitmap skelBitmap = new System.Drawing.Bitmap(640, 480)) 
     { 
      foreach (Skeleton S in skeletons) 
      { 
       if (S.TrackingState == SkeletonTrackingState.Tracked) 
       { 
        DrawBonesAndJoints(S,skelBitmap);       
       } 
       else if (S.TrackingState == SkeletonTrackingState.PositionOnly) 
       { 

       } 
      } 
      _videoArraySkel.Add(ToOpenCVImage<Bgr, Byte>(skelBitmap)); 
      BitmapSource source = ToWpfBitmap(skelBitmap); 
      this.skeletonStream.Source = source;  
     }    
    } 

和更精確的,讓我在我的窗口中顯示的ToWpfBitmap:

public static BitmapSource ToWpfBitmap(System.Drawing.Bitmap bitmap) 
    { 
     using (MemoryStream stream = new MemoryStream()) 
     { 
      bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp); 
      stream.Position = 0; 
      BitmapImage result = new BitmapImage(); 
      result.BeginInit(); 
      // According to MSDN, "The default OnDemand cache option retains access to the stream until the image is needed." 
      // Force the bitmap to load right now so we can dispose the stream. 
      result.CacheOption = BitmapCacheOption.OnLoad; 
      result.StreamSource = stream; 
      result.EndInit(); 
      result.Freeze(); 
      return result; 
     } 
    } 

的性能損失的特點是: - 在窗口中顯示的視頻不流暢了 - 視頻錄製似乎錯過了一些幀,導致視頻比正常速度更快/更低。

你能幫我告訴我這個問題可能來自哪裏嗎?

+0

也許大約[位圖的BitmapSource]這個CodeProject上的文章(http://www.codeproject.com/Articles/104929/Bitmap-to-BitmapSource)轉換可以幫助你。你有沒有嘗試過使用System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap方法? – Dirk

+0

我已經試過使用這種方法,但奇怪的是位圖不再顯示在我的窗口中了... –

回答

0

您是否嘗試過在單獨的線程中將內存寫入I/O,同時將數據保存在隊列中的緩衝區中?