Click here to Skip to main content
Click here to Skip to main content

C# \ VB.NET Camera Communication Libraries

, 15 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Stream a live camera video stream or single images between applications using the TCP protocol.
Prize winner in Competition "Best VB.NET article of September 2010"

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:

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

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

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:

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:

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):

Dim WithEvents Host As TCPCam.Host

On the Form_Load event handler, instantiate the class:

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:

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):

Dim WithEvents Client As TCPCam.Client

On the Form_Load event handler, instantiate the class:

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:

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:

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:

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)

About the Author

shynet
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

 
QuestionHelp: host as a server Pinmemberlccerna6-Mar-12 12:11 
AnswerRe: Help: host as a server Pinmembershynet8-Mar-12 4:07 
AnswerRe: Help: host as a server Pinmembermagic.on2-Jun-12 2:45 
GeneralRe: Help: host as a server Pinmembershynet2-Jun-12 9:27 
AnswerRe: Help: host as a server Pinmembermagic.on6-Jun-12 7:03 
QuestionTCP stream in web pages PinmemberIvanG1128-Feb-12 0:29 
AnswerRe: TCP stream in web pages Pinmembershynet1-Mar-12 5:01 
Questionvideo frame capture using media player Pinmember1siti19-Feb-12 21:05 
anyone know how to show video frame one by one when a video playing in media player.. plez, help me..
QuestionPlease let me know, How can I send sound? Pinmembernorachit11-Feb-12 15:03 
AnswerRe: Please let me know, How can I send sound? Pinmembershynet11-Feb-12 20:16 
QuestionWorks Locally Only and StartCamera() Error PinmemberMember 857528418-Jan-12 11:49 
AnswerRe: Works Locally Only and StartCamera() Error Pinmembershynet19-Jan-12 4:02 
GeneralRe: Works Locally Only and StartCamera() Error PinmemberMember 857528419-Jan-12 12:23 
GeneralRe: Works Locally Only and StartCamera() Error Pinmembershynet19-Jan-12 21:35 
GeneralRe: Works Locally Only and StartCamera() Error PinmemberTKCA22-Feb-12 5:06 
GeneralRe: Works Locally Only and StartCamera() Error Pinmembershynet23-Feb-12 2:30 
GeneralRe: Works Locally Only and StartCamera() Error PinmemberTKCA26-Feb-12 9:52 
GeneralRe: Works Locally Only and StartCamera() Error Pinmembershynet1-Mar-12 5:14 
GeneralRe: Works Locally Only and StartCamera() Error PinmemberMember 1042441626-Feb-14 4:15 
GeneralRe: Works Locally Only and StartCamera() Error Pinmembershynet27-Feb-14 6:27 
Questionplease, help me Pinmembere3d13c3-Oct-11 15:19 
AnswerRe: please, help me Pinmembershynet3-Oct-11 21:38 
QuestionHello,shynet!you can help me Again? Pinmembermricesnow28-Jul-11 18:17 
AnswerRe: Hello,shynet!you can help me Again? Pinmembershynet28-Jul-11 22:15 
Questionquestion of WPF PinmemberVince Chen61128-Jul-11 6:55 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 15 Dec 2011
Article Copyright 2010 by shynet
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid