croman102's picture

Using a web camera

Hello,

I know this is OpenTK forum and this question is more related to some other libraries, but I have been searching for an answer to this question and I am getting a bit frustrated.

So, I need image from webcam. There are bunch of different tutorials for getting webcam image to picturebox but I need a simple bitmap. The problem is I need to get it inside of my gameloop.
Something like this:

loop()
{
gamecode
more gamecode
gamecode
...
Bitmap image = cameraImage();
gamecode
...
}

So, is there any way to do this?

I have also found this topic http://www.opentk.com/node/561 here.
But the working example is no longer online.

Is there any chance of using AForge.net

Thank you!


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture
Quote:

There are bunch of different tutorials for getting webcam image to picturebox but I need a simple bitmap

For example this one:

The meat is in this part:

        private WebCamCapture webcam;
        private System.Windows.Forms.PictureBox _FrameImage;
        private int FrameNumber = 30;
        public void InitializeWebCam(ref System.Windows.Forms.PictureBox ImageControl)
        {
            webcam = new WebCamCapture();
            webcam.FrameNumber = ((ulong)(0ul));
            webcam.TimeToCapture_milliseconds = FrameNumber;
            webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
            _FrameImage = ImageControl;
        }
 
        void webcam_ImageCaptured(object source, WebcamEventArgs e)
        {
            _FrameImage.Image = e.WebCamImage;
        }

All you need to do is change that to:

        private WebCamCapture webcam;
        private Image FrameImage;
        private int FrameNumber = 30;
 
        public void InitializeWebCam()
        {
            webcam = new WebCamCapture();
            webcam.FrameNumber = ((ulong)(0ul));
            webcam.TimeToCapture_milliseconds = FrameNumber;
            webcam.ImageCaptured += new WebCamCapture.WebCamEventHandler(webcam_ImageCaptured);
        }
 
        void webcam_ImageCaptured(object source, WebcamEventArgs e)
        {
            // Dispose previous image to avoid memory leaks
            if (FrameImage != null)
            {
                FrameImage.Dispose();
            }
            FrameImage = e.WebCamImage;
        }

You can remove the rest of the WinForms stuff if you don't need that.

croman102's picture

Thank you

But I managed to do it myself using AForge:

private FilterInfoCollection videoCaptureDevices;
        private VideoCaptureDevice finalVideo;
        private Bitmap currentImage;
 
        public void setupWebCam()
        {
            videoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            finalVideo = new VideoCaptureDevice();
        }
 
        public void startRecording()
        {
            finalVideo = new VideoCaptureDevice(videoCaptureDevices[1].MonikerString);//I am using the second camera on my computer, if you only have one connected the index should be 0
 
            finalVideo.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
            finalVideo.Start();
        }
 
        private void videoSource_NewFrame(object sender, NewFrameEventArgs evantArgs)
        {
            currentImage = (Bitmap)evantArgs.Frame.Clone();
        }
 
        public Bitmap getCameraImage()
        {
            return currentImage;
        }

Thank you anyway.