A WebCam Class in Visual Basic

The WebCam class will make it easy for your applications to view a webcamera. Additionally, it will make it possible to configure a number of settings on the Webcam, such as setting the FPS value (frames per second).

This class was written quickly to dig into the .NET environment. As such, the coding may not be the best, but it works. Included with the code is a project that uses the class, so that you can see how simple it is.

If you look at the class, you will find that the first three variables that can be changed are as follows:

   Private CamFrameRate As Integer =  15
   Private OutputHeight As Integer = 240
   Private OutputWidth As Integer  = 360

CamFrameRate is set to 15 in this code. This is just the initial frame rate that is used. The CamFrameRate sets how much of a gap there is between frames. In this case, 15ms, or about 65 FPS, is the setting. You can change the FPS through a subroutine.

OutputHeight and OutputWidth are fairly self explanatory. You can set these to the dimensions of output.

Before doing much with a WebCam, you should probably make sure it is running. You can use the iRunning variable to check. This variable is defined in the code as follows:

Public iRunning As Boolean

You can call IRunningat any time; it will return true if the camera is running or false if it is not. For example, the following line of code displays a message box letting you know if the camera is running:

Messagebox.show Mycam.Irunning

How to Use the Class

To use the class, you first need to create an iCam object:

Private myCam As iCam
Set myCam = New iCam

From here, you can call a range of functions using standard syntax:

mycam.Function Name

Functions

The following provides a bit of information on the key functions:

  • initCam(ByVal parentH As Integer): This is where it all starts. You must call this to set up the camera. parentH is where you want to prievew, so if you have a pictureBox on your form, named picoutput, you would call initCam as follows:
  • myCam.initCam(Me.picOutput.Handle.ToInt32)
  • resetCam(): If you need to reset the camera, you can call this function. In general, you shouldn't need this.
  • setFrameRate(ByVal iRate As Long): With setFrameRate, you are able to set the frame rate by passing a FPS value. This value then will be converted into how much time there should be between frames. The following sets the frame rate using a value of 25:
  • myCam.setFrameRate(25)
  • closeCam(): You should always close out the webcam class when you are done. This helps to clear things up.
  • copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF): The copyFrame subroutine returns an image of the current frame. You need to pass to it the source picture box (where the camera image is) and then a rectangle specifying size dimensions:
  • Me.picStill.Image = myCam.copyFrame(Me.picOutput, _
                                        New RectangleF(0, 0, _
                                        Me.picOutput.Width, _
                                        Me.picOutput.Height))
    
  • FPS(): This FPS subroutine returns the current FPS value.

In Conclusion...

The code might not be the greatest; however, it works well. Hopefully, you will find it useful.



About the Author

Liam Bateman

I'm a Web/Applications Developer based in Manchester UK. Using C# / Vb (.Net And 6). Moderator At (www.Vbforums.com)

Downloads

Comments

  • "Not Declared" errors

    Posted by Skylar on 10/09/2012 03:20pm

    When I try to debug my app, I get these errors: 'Application' is not declared. It may be inaccessible due to its protection level. 'MessageBox' is not declared. It may be inaccessible due to its protection level. 'MessageBox' is not declared. It may be inaccessible due to its protection level. 'MessageBox' is not declared. It may be inaccessible due to its protection level. Type 'PictureBox' is not defined. Type 'RectangleF' is not defined. Type 'Bitmap' is not defined. Type 'Graphics' is not defined. Type 'Bitmap' is not defined. Type 'Graphics' is not defined. Type 'Bitmap' is not defined. 'MessageBox' is not declared. It may be inaccessible due to its protection level. Importing the namespaces do nothing, and are already referenced in my application anyways.

    Reply
  • Wifi

    Posted by zspzs on 03/19/2010 09:11pm

    Hello, Thanks for the nice article. It works fine when I use an USB webcam. Where can I change the webcam source in the code if I want to use another camera? e.g. wifi camera. Thank you in advance for your help! Peter

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 18, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT While the idea of using facial and or gesture recognitions to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development. There are many different ways in which Intel and Unity Technologies have been working together to helps speed the develop of games with the Intel® RealSense™ SDK (Software Developer Kit), so come hear from a panel of experts on what we've done …

  • On-demand Event Event Date: March 19, 2015 The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private app stores to …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date