C# \ VB .net Camera communication libraries

Uploaded at 16.09.2010, by Shynet (viewed 16006 times)
I got an email today about the nBot project - and I was asked how did I passed the webcam stream to another application
written in C# \ VB .net without any lags?

To be honest, when I first started the nBot project I had the same issues - how can I send a camera stream
through LAN between two application developed under C# \ VB .net?

I wanted the connection to have no delay (or at least not any noticeable delay) and to be easy
to code and use, for any one who needs it.

I did a small research about this issue and haven't found any solution, I only found
"pieces" of ideas and ways of doing it. I couldn't find any free of charge written library.

So I tried to connect all of the pieces and after completing the research
I created my own ActiveX (control) to create this kind of communication.

I worked on this ActiveX and written the classes for a couple of hours, and testing it
in the nBot project until I found it was working perfectly for our project. The library
uses the TCP protocol and should work well with your LAN.

This ActiveX has been developed with VB .net and can be used in C# or VB .net.

To download this ActiveX/Library click on this link.

**I used the ICam class developed by Pino to capture the camera frames, you can find the original ICam class at this link: http://www.vbforums.com/showthread.php?p=2048466 - in this project I modified the ICam to fit my needs -- go visit his site: http://www.liambateman.net/**

The link above is the whole camera stream written part of the project including the source codes (which you can have a look at).

See the activex in action on the nBot project video:

TCPCam - Camera communication library

This library can be used in 2 ways: as a control (like a button) that you can drag and drop to your project, or just like a normal class.
I would recommend on using this class as an ActiveX which makes it alot easier. This library uses port 2020 (TCP) by default to create the communication.

The concept

The client application sends the frames captured by the camera (connected to the client computer) to the host, which receives the images and updates the control image which creates the "video" stream effect.

The ActiveX

Extract the "TCPCam.rar", you should find this directory:

Enter to your project and do the following:
1. Open the Toolbox and right click on an empty space in the toolbox.
2. Select “Choose items...” – a dialog should pop up
3. Enter the “.Net Framework Components” tab, and click on “Browse...”
go to the extracted directory path I mentioned earlier and import this file “TCPCamActivex.dll”.
4. Accept the dialogs.
5. Now just drag the “TCPClientActivex” control to your form, this is where your camera frames are shown.

Make it work
I called the ActiveX in the same name on both client and host applications - "Webcam".

On the host side
application we should start listening so we would use this
methods on the Form_Load event handling method:
When you close the application make sure to close the listening thread:
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) event handling method do the following:
Make sure to connect first and then start the camera!
When you finished and want to stop the connection do the following:

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 was succesfully made the control raises the Connected event. Every time a connection ended the control raises the Disconnected event.

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

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

The TCPCam Class

Instead of using the TCPCam ActiveX you can use the TCPCam class which can be found on the extracted rar file on this path:
"TCPCam\TCPCam\TCPCam\bin\Release" just add a reference to your project with the "TCPCam.dll" file. You need a picturebox to display the camera frames on. Declare your camera object before you continue:
Dim Webcam As New TCPCam.Host(Picturebox,Port)
or as a client:
Dim Webcam As New TCPCam.Client(Picturebox)
With this class you can use the SendImage method to send images to the host, not just camera frames. I will post more information about the class if anyone will want.

I will be happy to hear your feedback's and hope it helps to any of you,
just leave your comment below.

Comments (11)

Leave a comment
{{ comment.author }}, at {{ comment.date }}
{{ comment.content }}