Click here to Skip to main content
15,867,330 members
Articles / Multimedia / Video

C# \ VB.NET Camera Communication Libraries

Rate me:
Please Sign up or sign in to vote.
4.95/5 (77 votes)
15 Dec 2011CPOL6 min read 1M   56.9K   220   210
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)


Written By
Software Developer Nemex Studios
Israel Israel
My name is Shay and I'm 21 years old.
At age 16 I created Nemex Studios (www.byshynet.com), an application development "company" (not official).
I'm the developer of "Mouse Recorder Pro", "Mouse Shaker" and many other applications, developed to help the community.

Comments and Discussions

 
QuestionIP camera in vb.net that require password Pin
Member 1451250926-Jun-19 21:22
Member 1451250926-Jun-19 21:22 
Questionconnect from home to work Pin
Member 1332202116-Sep-17 22:29
Member 1332202116-Sep-17 22:29 
QuestionIncluding audio / Select camera option not working Pin
Member 1329631626-Jul-17 4:44
Member 1329631626-Jul-17 4:44 
QuestionTwo-Way Feed Pin
Member 1302424618-Mar-17 23:40
Member 1302424618-Mar-17 23:40 
QuestionI am getting an error that it cannot include the ".tld" file while i am including the .dll file ! what do i do ? Pin
KAAAAATE 00926-Feb-16 4:23
KAAAAATE 00926-Feb-16 4:23 
QuestionHow about audio ? Pin
amr ismail21-Mar-15 11:09
amr ismail21-Mar-15 11:09 
QuestionI can't use 2 webcam together, 1 is running, 1 is green Pin
Phạm Thành6-Nov-14 18:40
Phạm Thành6-Nov-14 18:40 
AnswerRe: I can't use 2 webcam together, 1 is running, 1 is green Pin
shynet7-Nov-14 8:16
shynet7-Nov-14 8:16 
GeneralRe: I can't use 2 webcam together, 1 is running, 1 is green Pin
Phạm Thành7-Nov-14 8:53
Phạm Thành7-Nov-14 8:53 
GeneralRe: I can't use 2 webcam together, 1 is running, 1 is green Pin
shynet8-Nov-14 4:30
shynet8-Nov-14 4:30 
GeneralRe: I can't use 2 webcam together, 1 is running, 1 is green Pin
Phạm Thành8-Nov-14 4:35
Phạm Thành8-Nov-14 4:35 
QuestionMultiple Clients Send the live video to one host Pin
Member 43236594-Nov-14 0:05
Member 43236594-Nov-14 0:05 
AnswerRe: Multiple Clients Send the live video to one host Pin
shynet4-Nov-14 10:58
shynet4-Nov-14 10:58 
Hello there!

1. I'm sorry but currently this library only support a host + client connection (only 2 sides).
2. Since you cannot work with more then 2 endpoints there is nothing you can't do.

What you can do, is edit this library source code, which should be a bit tricky but should work.

Shay.
GeneralRe: Multiple Clients Send the live video to one host Pin
Member 43236594-Nov-14 22:02
Member 43236594-Nov-14 22:02 
GeneralRe: Multiple Clients Send the live video to one host Pin
shynet4-Nov-14 23:05
shynet4-Nov-14 23:05 
SuggestionCamera libraray options Pin
David T. Kerrigan1-Oct-14 21:26
David T. Kerrigan1-Oct-14 21:26 
QuestionTCP camera? Pin
Madness Fading4-Sep-14 14:46
Madness Fading4-Sep-14 14:46 
QuestionHow can I connect a IP-camera which needs username/password Pin
Elvind20-Jan-14 1:04
Elvind20-Jan-14 1:04 
AnswerRe: How can I connect a IP-camera which needs username/password Pin
shynet24-Jan-14 4:48
shynet24-Jan-14 4:48 
QuestionHI ... "Error setting up camera"? Pin
Darksidek16-Dec-13 4:42
Darksidek16-Dec-13 4:42 
AnswerRe: HI ... "Error setting up camera"? Pin
shynet20-Dec-13 10:27
shynet20-Dec-13 10:27 
GeneralRe: HI ... "Error setting up camera"? Pin
Darksidek2-Jan-14 4:53
Darksidek2-Jan-14 4:53 
GeneralRe: HI ... "Error setting up camera"? Pin
shynet2-Jan-14 22:20
shynet2-Jan-14 22:20 
GeneralRe: HI ... "Error setting up camera"? Pin
Darksidek6-Jan-14 0:43
Darksidek6-Jan-14 0:43 
GeneralRe: HI ... "Error setting up camera"? Pin
shynet9-Jan-14 23:59
shynet9-Jan-14 23:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.