How to capture a constant stream of bitmaps from a webcam in C #

We have a C # application that processes video streams. This is a low-level application that receives every frame in Bitmap format, so basically we need 25 images per second. This application already works for some of our media sources, but now we need to add a webcam as an input device.

Thus, we basically need to constantly capture bitmaps from a webcam so that we can transfer all these frames as a β€œstream” to our application.

What is the best and easiest way to access the webcam and view actual frames directly from the webcam as separate images? I'm still in the starting blocks.

There are many libraries that allow you to access the webcam, view the contents of the webcam in the Windows panel, and then use screen capture to capture the image again. This, unfortunately, will not give us the necessary performance when capturing 25 frames per second. IVMRWindowlessControl9 :: GetCurrentImage is referred to as another alternative, but again it seems to be aimed at an infrequent snapshot rather than a constant stream of images. Directshow.Net is mentioned by many as a good candidate, but it is unclear how easy it is to capture images from a webcam. In addition, many sources claim that Microsoft no longer supports Directshow. In addition, the implementations I saw from this require ImageGrabber, which apparently also is no longer supported . The newer alternative to MS is the Media Foundation, but in my research there are no working examples of how this can be implemented (and I'm not sure if this will work on older versions of Windows such as XP). DirectX.Capture is a terrific library ( see a nice implementation ), but it seems that there are not enough filters and methods for directly receiving video images. I also started looking at Filters and Filters , but it seems terribly complicated and a bit like reinventing the wheel.

In general, all the solutions briefly mentioned above seem rather old. Can someone point me in the direction of a walkthrough to get a webcam running in C # and capture a few images per second from it? (We will also need to make the sound at some point, so the solution that does not exclude the video will be most useful).

+4
source share
2 answers

I use AForge.Video (find it here: code.google.com/p/aforge/) because it is a very fast C # implementation. I am very pleased with the performance and effortlessly captures two HD webcams at 30 frames per second on an 8-year-old PC. data is transmitted as native IntPtr, so it is ideal for further processing using native code or opencv.

opencv wrappers emgu and opencvsharp provide a rudimentary video capture feature that may be sufficient for your purposes. obviously, if you are going to do image processing / computer vision, you can use them anyway.

+4
source

As suggested by dr.mo, Afoge was the answer.

I used the tutorial here: http://en.code-bude.net/2013/01/02/how-to-easily-record-from-a-webcam-in-c/

In the tutorial, they have an event handler fire every time a frame is received from a webcam. In the original tutorial, this bitmap is used to write the image to a PictureBox. I just changed it to save the bitmap to a file, not to an image. So I replaced the following code:

pictureBoxVideo.BackgroundImage = (Bitmap)eventArgs.Frame.Clone(); 

with the following code:

 Bitmap myImage = (Bitmap)eventArgs.Frame.Clone(); string strGrabFileName = String.Format("C:\\My_folder\\Snapshot_{0:yyyyMMdd_hhmmss.fff}.bmp", DateTime.Now); myImage.Save(strGrabFileName, System.Drawing.Imaging.ImageFormat.Bmp); 

and it works like a charm!

+3
source

Source: https://habr.com/ru/post/1478968/


All Articles