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:
However, the problem comes when I try to continually grab images and update them. If I use code such as:
Image LiveImage = Camera.GetImage(); // Grab the camera's image
PictureBoxLive.Image = LiveImage(); // Display it on the 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?
for( ;; ) // Loop forever
UpdateLiveImage(); Continually grab image and dispaly on form
I tried using threading to solve the problem, with the code:
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".
Thread t = new Thread(UpdateLiveImage);
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!