繁体   English   中英

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

[英]displaying video frame by frame increases cpu usage

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

公共局部类MainWindow:窗口{TcpClient客户端; NetworkStream ns; 线程视频帧; 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; }));

    }

}

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

对WriteableBitmap上的异步操作的第一个答案进一步阐述。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM