1

I am receiving video from kinect device. Server is sending video frame by frame and on the client side it receives frame but starts flickering on image control if I use BitmapSource. Create function which is responsible of increasing CPU usage after that i use WriteableBitmap class but I'm stuck into a new problem, it is giving me error "the calling thread cannot access the object but different thread own it", i use dispather.invoke to solve the problem but it is giving me the same error.

public partial class MainWindow : Window { TcpClient client; NetworkStream ns; Thread 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; }));

    }

}
sonne
  • 377
  • 5
  • 20
Ali
  • 557
  • 1
  • 9
  • 30

1 Answers1

0

The problem is that you're creating the WriteableBitmap in your background thread. It needs to be created on the UI thread, and you would want to pass the data to the UI thread to update the bitmap.

The first answer to Asynchronous operations on WriteableBitmap elaborates further.

Community
  • 1
  • 1
Abe Heidebrecht
  • 30,090
  • 7
  • 62
  • 66
  • it doesn't solve my problem if i create WritableableBitmap in UI thread my cpu usage increases with results in filckering of video – Ali Jun 28 '13 at 10:07