5

I'm in dire need of a replacement for a wrapper being used in a C# application. Basically, what we need to do is attach a webcam feed to one of two picture boxes. This will be used to take still images at the push of a button, which may detach the camera feed and attach the still image to that picture box, then reattach the camera feed later. We had previously found some free code to utilize with a CaptureDevice.cs file and Pinvoke.dll to tie it into avicap32.dll. Unfortunately, this seems to have random, intermittent errors that cannot be reliably reproduced. It's just too flaky. At some random point, one of those picture boxes may go black and won't show the feed until the picture is taken, at which point the proper picture is attached to the picture box. Then, even if there's only one webcam attached, it'll keep prompting for the webcam to be selected, something it wouldn't do otherwise.

Quite frankly, I'm surprised and dismayed that Microsoft hasn't included anything in .NET to cover webcam video feeds. I'm looking for something reliable and relatively simple to implement to replace this buggy webcam system.

user1017413
  • 2,023
  • 4
  • 26
  • 41
  • Is a commercial library an option ? – Yahia Feb 02 '12 at 20:51
  • Some of the links in this post might help: http://stackoverflow.com/questions/833724/c-sharp-directshow-net-simple-webcam-access – CodingWithSpike Feb 02 '12 at 20:51
  • There is also some sample code here: http://www.c-sharpcorner.com/UploadFile/iersoy/how-to-use-webcam-in-xna/ that is XNA related, but might work for you. It basically just taps into DirectShow (part of DirectX). – CodingWithSpike Feb 02 '12 at 20:55
  • Sorry Yahia, but commercial is out. This application will probably wind up being free and open sourced. – user1017413 Feb 02 '12 at 20:57
  • 1
    @user1017413 Check my answer, I added a source project with camera working. – corylulu Feb 02 '12 at 21:06

3 Answers3

2

It is time to use MediaCapture object. Use this sample for MS Windows 10 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/CameraStarterKit/

And read this article as well http://www.codepool.biz/csharp-camera-api-video-frame.html

NoWar
  • 36,338
  • 80
  • 323
  • 498
1

May I suggest

http://www.emgu.com/wiki/index.php/Main_Page

I have used OpenCV in many C++ libraries and it seems to work very well for webcams from other things I've tried. Emgu is just a C# wrapper for OpenCV.

Here is a sample project to try it out on. It's very basic and simple, but should work right away.

http://dl.dropbox.com/u/18919663/vs%20samples/OpenCVCSharpTest.zip (just uploaded)

Sample:

using Emgu.CV;
using Emgu.CV.Structure;
...

public partial class Form1 : Form
{
    public Capture cvWebCam;
    public Form1()
    {
        InitializeComponent();

        try
        {
            cvWebCam = new Capture();
            timer1.Start();
        }
        catch
        {
            Console.WriteLine("Default camera not found or could not start");
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (cvWebCam != null)
        using (Emgu.CV.Image<Bgr, byte> frame = cvWebCam.QueryFrame())
        {
            pictureBox1.BackgroundImage = frame.ToBitmap();
        }
    }
}
corylulu
  • 3,449
  • 1
  • 19
  • 35
0

Try DirectShow.net- it's a free wrapper library to access DirectShow functionality from .NET: http://directshownet.sourceforge.net

Its code samples contain a sample app for capturing video from webcams, too: http://sourceforge.net/projects/directshownet/files/DirectShowSamples/

tpolyak
  • 1,234
  • 1
  • 9
  • 15