我正在從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; }));
}
}
嘗試使用VLC其幻想,搜索nVLC。lib封裝的libVLC – ahmedsafan86