逐帧显示视频会增加CPU使用率

时间:2013-06-27 20:38:44

标签: c# wpf multithreading kinect

我正在接收来自kinect设备的视频。服务器逐帧发送视频,在客户端,它接收帧但如果我使用BitmapSource则开始在图像控制上闪烁。在我使用WriteableBitmap类之后创建负责增加CPU使用率的函数但是我遇到了一个新问题,它给了我错误“调用线程无法访问对象但是不同的线程拥有它”,我使用了dispather.invoke解决问题,但它给了我同样的错误。

public partial class MainWindow:Window     {         TcpClient客户端;         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; }));

    }

}

1 个答案:

答案 0 :(得分:0)

问题是您在后台线程中创建了WriteableBitmap。它需要在UI线程上创建,并且您希望将数据传递给UI线程以更新位图。

Asynchronous operations on WriteableBitmap的第一个答案进一步阐述。

相关问题