Thread: Displaying Streaming Video on a Form

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    36

    Displaying Streaming Video on a Form

    Hello,

    I am writing a program in C#, which displays a streaming video from a webcam. My code uses the Windows Image Acquisition library, and I have so far been able to grab a single frame from the camera and display it on my Windows Form, by using the function UpdateLiveImage:

    Code:
    void UpdateLiveImage()
    {
            Image LiveImage = Camera.GetImage(); // Grab the camera's image
            PictureBoxLive.Image = LiveImage(); // Display it on the form
    }
    However, the problem comes when I try to continually grab images and update them. If I use code such as:

    Code:
    for( ;; )  // Loop forever
    {
            UpdateLiveImage(); Continually grab image and dispaly on form
    }
    This runs without any errors, but it seems that updating the PictureBox only occurs once this function has returned. If i use a for loop such as for(int i = 0; i < 100; i ++) and move the camera around whilst this loop is being executed, then I only ever get the last image to be taken, displayed on the PictureBox. Is there any way to update the picture box while this function is still running in a loop?

    I tried using threading to solve the problem, with the code:

    Code:
    {
            Thread t = new Thread(UpdateLiveImage);
            t.Start();
    }
    but this caused an InvalidCastException exception, with something to do with the Windows Image Acquisition library. The error is "Unable to cast COM object of type 'WIAVIDEOLib.WiaVideoClass' to interface type 'WIAVIDEOLib.IWiaVideo'. This operation failed because the QueryInterface call on the COM component for the interface with IID".

    I have no idea what this means, but it seems that Windows Image Acquisition doesn't like threading, and so I am trying to do this without the use of threads in this way. Any ideas? Alternatively, the way I have set up the thread is wrong, in which case I'd be grateful if anyone could tell me!

    Thank you!!
    Ed.

  2. #2
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    in your for loop try putting in a PictureBox.Refresh(); and see if it redraws it.
    Code:
    for( ;; )  // Loop forever
    {
            UpdateLiveImage(); //Continually grab image and dispaly on form
            PictureBoxLive.Refresh();
    }
    although, this loop will still be pretty nasty and cause the app to focus mainly on that box and make the app look unresponsive. to get past that you could add a

    Code:
                Application.DoEvents();
    to your for loop to make it still respond.

    or have a timer that will get a new image and update your picture box at a set interval.

    there is also a threadsafe thing you can turn off in c# that might help.

    Code:
    CheckForIllegalCrossThreadCalls = false;
    you can try that and see if it helps. but it doenst sound like thats the error you are getting.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  3. #3
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    Another library I have used quite a bit in C++ is the "Video For Windows" library. I have not used it in c# but I am sure its not too different. It was very flexible in c++ though and you didn't have to have a separate thread to get it to stream.
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  4. #4
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    forgot to add a link.

    here is an example online i found for c#. It is not my example because as i have said, i have not messed with it in c# but i downloaded it and it compiled and used my little webcam in vista so I am sure it most likely works anywhere else.

    http://www.geocities.com/egon_rath/avicap.html
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying values of variables to a Form
    By Cielo in forum C++ Programming
    Replies: 1
    Last Post: 03-07-2008, 03:47 AM
  2. Streaming video from ip camera
    By fogald in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2007, 02:52 PM
  3. ascii characters video displaying on tv screen
    By deian in forum C Programming
    Replies: 6
    Last Post: 10-12-2004, 09:46 PM
  4. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM