Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / video

C# VB.NET Camera Communication Libraries

4.95/5 (77 votes)
15 Dec 2011CPOL6 min read 1.1M   57.1K  
Stream a live camera video stream or single images between applications using the TCP protocol.

TCPCamScreenshotSmall.jpg

Introduction

I have seen a lot of people searching for a library capable of streaming a live camera video between two applications through the LAN or any other kind of network.

I myself needed it for my school project - but I couldn't find any free working library on the web, so I did some research and connected all of the different pieces to create this library.

This library allows you stream your camera video to another application written using the .NET Framework.

Although this article is written in VB.NET - you can use it with your C# projects too!

The camera stream should work well without any noticeable delays on your Local Area Network, but will lag when using it with an internet connection.

I used the ICam class developed by Pino to capture the camera frames; you can find the original ICam class here: http://www.vbforums.com/showthread.php?p=2048466 (check out his site). In this project, I modified the ICam to fit my needs.

Background

This library includes the camera capture class and the camera stream class. I separated this library to into two different ways of usage:

  1. As an ActiveX (a control) which allows you to drag and drop it to your form like any other control - including the host and the client method in the same control.
  2. As two classes; client and host, including some advanced methods for advanced users.

The main concept of this library is that the client that is connected to the image source (camera) sends the frames to the host (serializes the frame and then sends it); the host deserializes the image and updates the PictureBox/control.

Using the code - As an ActiveX

Use this if you want to stream your video in just a few short steps, no need for a lot of programming - drag and drop the control and write a little code.

Extract the "TCPCam.zip" attached, and you should find this directory: "TCPCamActivex\TCPCamActivex\bin\Release".

In your project, do the following:

  1. Open the Toolbox and right click on an empty space in the toolbox.
  2. Click "Choose items..." and a dialog should pop up.
  3. Enter the ".NET Framework Components" tab, and click on "Browse...".
  4. Go to the extracted directory path I mentioned earlier and import this file: TCPCamActivex.dll.
  5. Accept the dialogs.
  6. Now just drag the TCPClientActivex control to your form; this is where your camera frames are shown.

I called the ActiveX the same name on both the client and host applications - "Webcam". This ActiveX uses port 2020 by default.

On the host side application, we should start listening to any incoming connection, so we would use this method on the Form_Load event handler:

VB
Webcam.StartListening() 'Opens the connection to accept clients

When you close the application, make sure to close the listening thread:

VB
Webcam.StopListening() 'Closes the connection and does not accept clients

On the client side application, we should first connect the capture source (the camera). On the Form_Load event (or any other event you want) handler, do the following:

VB
Webcam.Connect(IPAddress) 'Connects to the host (Replace the IPAddress variable)
Webcam.StartCamera() 'Starts up the camera to capture the video stream

Make sure to connect first and then start the camera! When you are finished and want to stop the connection and close the camera, do the following:

VB
Webcam.Disconnect() 'Disconnects any open connection
Webcam.StopCamera() 'Stops the camera capture

ActiveX Events

This ActiveX comes with the following events:

  • OnFrameDraw(ByVal e As System.Windows.Forms.PaintEventArgs)
  • Connected()
  • Disconnected()
  • OnConnection()
  • LostConnection()

The OnFrameDraw event comes with the e as PaintEventArgs argument, which allows you to draw directly on the frame, and can be used on both sides.

The Connected and Disconnected events are only fired on the client side; every time a connection is successfully made, the control raises the Connected event. Every time a connection ends, the control raises the Disconnected event.

The OnConnection and LostConnection events are only fired on the host side; every time a client is connected, the OnConnection event is raised. Every time a client disconnects, the LostConnection event is raised.

You can handle these events to monitor your connection status and draw on the camera frames with GDI.

Using the code - As a class (Advanced)

Use this if you want to have some more control - like sending your own images, and choosing the PictureBox you want the frames to be drawn on.

Extract the "TCPCam.zip" file attached, and you should find this directory: "TCPCam\TCPCam\bin\Release".

On the host side, you should have a PictureBox control for the frames to be drawn on, and declare the TCPHost object (as a global variable):

VB
Dim WithEvents Host As TCPCam.Host

On the Form_Load event handler, instantiate the class:

VB
Host = New TCPCam.Host(PictureBox1, 8080)

PictureBox1 points to the PictureBox control you have on your form; 8080 is the port used in the connection.

Now add the following after the code above:

VB
Host.StartConnection() 'Opens the connection to accept clients

On the client side, you should have a PictureBox control for the camera frames capture to be drawn on, and declare the TCPClient object (as a global variable):

VB
Dim WithEvents Client As TCPCam.Client

On the Form_Load event handler, instantiate the class:

VB
Client = New TCPCam.Client(PictureBox1) 'Set to null if you only you want to send images
Client.CameraFPS = 30 'The number of frames per second (refresh rate)
Client.CameraOutputSize = PictureBox1.Size 'Set the output size to the Picturebox size
Client.StartCamera() 'Start the camera video stream

Now we need to connect to the host, create a textbox or anything you want to get the IP address, create a button (the connection button), and in the Button_Click method, add the following:

VB
Client.Connect(TextBox1.Text, 8080)

Replace Textbox1.Text with the IP address; 8080 is the port number the host uses.

You can use this class to transfer images instead of sending a video stream, just set the Picturebox1 variable to Null and don't use the CameraStart method. Every time you want to send an image, use the following method:

VB
Client.SendImage(Image) 'Image is the image object to send

Class events 

The host comes with the following events:

  • OnConnection()
  • LostConnection()
  • errEncounter(ByVal ex As System.Exception)

The onConnection and LostConnection events are raised when a connection was created or a connection is lost, respectively. errEncounter is raised when an error occurs.

The client comes with the following events:

  • Connected()
  • Disconnected()
  • errEncounter(ByVal ex As System.Exception)

The Connected event is raised when a connection is successfully made, and the Disconnected event is raised when a disconnection occurs. errEncounter is raised when an error occurs.

BertMan Recordable TCPCam 

BertMan took the TCPCam library and added a recording capability at the client side. Download BertManRecordableTCPCam.zip to use TCPCam with client recording capability. 

The modified code provides the StartRecording method to start a recording:

VB.NET
Client.CapturePathAndFileName = "c:\videos\myvideo.avi" 'The path of the recorded file
Client.StartRecording() 'Start the camera recording 

And use the StopRecording method to stop a recording:

Client.StopRecording()  

Thank you so much BertMan for sharing this modified TCPCam code and helping others! 

Points of interest 

You can use the TCPCam library to transfer your camera video stream very easily from one application to another. Use the ActiveX to transfer the video stream easily, or use the classes to use some more advanced methods and send single images.

Using the ActiveX, you can draw on your video frames by handling the OnFrameDraw event and with some GDI coding.

Using this as a class, you can use OnPaint to draw on a new frame. This can be useful if you want to draw some graphics on your frame.

I have been searching for this kind of a library a lot, and I couldn't manage to find one - so I built my own. Hope it helps!

nBotCamShot600.jpg

Using the TCPCam as an Activex to draw a weapon sight and remote control the nBot.

History 

  • 15.12.11 - Added BertManRecordableTCPCam.zip. 
  • 26.09.10 - Added an example picture.
  • 27.09.10 - Changed the description to match the library.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)