jump to navigation

Webcam in C#: with OpenCVDotNet December 12, 2008

Posted by haryoktav in Taipei.
trackback

This is a simple way to access your webcam in C# (C Sharp) using openCVdotnet. You can follow this link http://code.google.com/p/opencvdotnet/wiki/Install

  1. Make sure you have installed Visual C# already (I am using 2005).
  2. Install openCV from http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=16937 (choose openCV 1.0, its library match with openCVdotnet wrapper).
  3. Install openCVdotnet from http://code.google.com/p/opencvdotnet/downloads/list

When you install  openCVdotnet, if it can’t find library in C:\Program Files\OpenCV\bin\cv100.dll, that means you installed wrong version of openCV.

And now for the code.

Just follow this link http://code.google.com/p/opencvdotnet/wiki/Tutorial, it works well for me.

  • Don’t forget to add Reference from openCVdotnet library which are you have just installed.
  • Don’t forget to add “using OpenCVDotNet;” (without brackets).
  • Put a button, a timer, and a picturebox (use stretch image property).

Here we are:

  • When form is active, assign a variable to handle the webcam and start the timer.
  • When the timer event is fired, get the frame and show the image to the picturebox.

The complete code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenCVDotNet;                 //add here

namespace Webcam1
{
    public partial class Form1 : Form
    {
        private CVCapture capture;  //variable here

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            capture = new CVCapture(0); //0 means current webcam attaced
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = !(timer1.Enabled); // toggle start/stop
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            using (CVImage nextFrame = capture.QueryFrame())
            {
                pictureBox1.Image = nextFrame.ToBitmap();
            }
        }

    }
}

The result:

But I don’t know how to de-activate my webcam 🙂 And just remember, even you stretch the image size, it is still saved as a normal size in its buffer/memory.

Here is the project. That is a rar-file, don’t forget to rename the file from “.doc” to “.rar” after download.

Captured

My room's view was captured

Advertisements

Comments»

1. lenin enriquez - February 4, 2009

que mas colegas sera posible que nos envien los programas complementarios para el proyecto de la camara web con c#

GRACIAS

2. haryoktav - March 9, 2009

#lenin enriquez
I added the project (above the image), hope meet your need. Thanks.
By the way, if you find difficulties using OpenCVDotNet I suggest you to use another wrapper e.g. AForge. Maybe I will use aForge too 🙂

3. Rahul - March 15, 2009

I kEep getting this error message at run time.

Could not load file or assembly ‘OpenCVDotNet, Version=1.0.2615.24735, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

I have tried setting

, reinstalling openCV and Opencvdotnet.

I’m using VS2008 and its a WPF application.

4. haryoktav - March 17, 2009

#Rahul
I already forgot about this. I am also a beginner.
1. Did you install OpenCV_1.0.exe?
2. Did you install opencvdotnet-0.7-setup.exe?
3. I am using VS2005
4. had you added OpenCVDotNet.dll in your project solution as References? (Right click on Solution Explorer – Add Reference)
5. Did you write: using OpenCVDotNet; in your program?

I hope everything goes well..

5. Alexander - March 19, 2009

I can this error message=”Object reference not set to an instance of an object.” — “CVImage nextFrame = capture.QueryFrame()”

6. haryoktav - March 20, 2009

#Alexander
I have no idea about that 🙂
I hope if you copy my project and do the fourth-comment then you will have fun with your webcam.
Actually, I also have some difficulties about webcam, if I don’t use webcam installed on my laptop, e.g. using USB camera, my program failed to find the camera. Because I can not find further documentation, I switch to AForge.. but it also not easy.. pphhfff…

7. JJ - April 27, 2009

Rahul..
I already did what u advised in the comment no 4.. but still go the same error. Could not load file or assembly ‘OpenCVDotNet, Version=1.0.2615.24735, Culture=neutral, PublicKeyToken=null’

im realy stucked at this for the whole day.. hope you can help me solve it.

8. JJ - April 27, 2009

oops sorry i mean haryoktav.. 🙂

9. haryoktav - May 1, 2009

#JJ
Wow, that is weird..
I already left my OpenCVDotNet.. I switch to AForge.NET, it provides many functions. See my posting at https://haryoktav.wordpress.com/2009/03/21/webcam-in-c-aforgenet/
Sorry JJ, I can not help you further.

10. Putu - May 16, 2009

Hello, i’m Putu
I have the same problems like Rahul..
and i’m also using vs2008, is that possible if the dotnet wrapper only work out with vs2005, it didnt work at vs2008??

haryoktav - May 24, 2009

#Putu
Thanks for visiting.
Sorry, I have no idea about this 🙂

11. Ely - July 3, 2009

Rahul and JJ

Did you solve your problem?
I have the same error message at run time and I would like to know if there is something to do…

I would appretiate too much your answer. 😉

12. Ely - July 14, 2009

I found in another web page that it’s necessary to install Visual C++ 2005 Express… I did it and it solved my problem… good luck for everybody.

13. sebastian - July 17, 2009

No se de donde descargar la libreria OpenCVDotNet;
si alguien me puede ayudar agradecido

14. haryoktav - August 5, 2009

#sebastian
usted puede descargar aquí la biblioteca, http://code.google.com/p/opencvdotnet/

15. minhtri - February 19, 2011

help me!, it don’t active, error:pictureBox1.Image = nextFrame.ToBitmap();
tooltip:<Object reference not set to an instance of an object.

16. naresh - May 19, 2011

Could not load file or assembly ‘OpenCVDotNet, Version=1.0.3103.29996, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail. (Exception from HRESULT: 0x800736B1)

17. Van Hien - November 2, 2011

I use USB camera plug into USB port of Coputer.
Then i copy above code then paste Form1 C# , then i run it error.
my Error at this line :

selectPictureBox1.Image = nextFrame.ToBitmap();

Error show : ” Object reference not set to an instance of an object.”

How does fix it ???

18. Van Hien - November 2, 2011

Why do not get camera frame?That why Picturebox.image = null

19. Essa - March 16, 2012

Could not load file or assembly ‘OpenCVDotNet, Version=1.0.2615.24735, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log or use the command-line sxstrace.exe tool for more detail. (Exception from HRESULT: 0x800736B1)

please need solution 😦

20. Amogh Patil - December 24, 2012

thank u worked like charm!!!!!!!!!!!!!!!!!! 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: