2013-06-27 41 views
1

我正在從kinect設備接收視頻。服務器逐幀發送視頻,並在客戶端接收幀,但如果使用BitmapSource,則在圖像控制上開始閃爍。創建函數負責增加CPU使用率之後,我使用WriteableBitmap類,但我陷入一個新的問題,它給我錯誤「調用線程不能訪問對象,但不同的線程擁有它」,我使用dispather.invoke解決問題,但它給了我同樣的錯誤。逐幀顯示視頻增加了CPU使用率

public partial class MainWindow:Window { TcpClient client; NetworkStream ns; 線程vedioframe; WriteableBitmap vediofram = null;

public MainWindow() 
    { 
     InitializeComponent(); 
     client = new TcpClient(); 
     client.Connect("127.0.0.1", 9000); 
     vedioframe = new Thread(Display_Frame); 
     vedioframe.Start(); 


    } 
    public void Display_Frame() 
    { 

     ns = client.GetStream();  
     while (true) 
     { 
      byte[] vedio = new byte[1228800]; 
      ns.Read(vedio, 0, vedio.Length); 
      try 
      { 
       if (vediofram == null) 
       { 
        vediofram = new WriteableBitmap(640, 480, 96, 96, PixelFormats.Bgr32, null); 

       } 
       else 
       { 

        vediofram.WritePixels(new Int32Rect(0, 0, 640, 480), vedio, 640 * 4, 0); 

       } 
       Update_Frame(vediofram); 

      } 
      catch (Exception e) 
      { 
       MessageBox.Show(e.Message); 
      } 

      // Dispatcher.Invoke(new Action(() => { BitmapSource s = BitmapSource.Create(640, 480, 96, 96, PixelFormats.Bgr32, null, vedio, 640 * 4); 
      // Vedio.Source = s; 
      /// })); 

     } 
    } 
    void Update_Frame(WriteableBitmap src) 
    { 
     Dispatcher.Invoke(new Action(() => { Vedio.Source = src; })); 

    } 

} 
+0

嘗試使用VLC其幻想,搜索nVLC。lib封裝的libVLC – ahmedsafan86

回答

0

問題是您在後臺線程中創建WriteableBitmap。它需要在UI線程上創建,並且您希望將數據傳遞到UI線程以更新位圖。

Asynchronous operations on WriteableBitmap的第一個答案進一步闡述。

+0

它不能解決我的問題,如果我在UI線程中創建WritableableBitmap我的CPU使用率增加與結果filckering的視頻 – Ali