Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Monitor Your Web Cam from a Remote Computer

We've offered a few solutions for working with web cams within .NET to create fun and intriguing monitoring applications. In this article, we extend those ideas so that web cam images can be shared with multiple clients over the Web.




Application Security Testing: An Integral Part of DevOps

n two of my earlier articles for DevX, "Teach Your Old Web Cam New Tricks: Use Video Captures in Your .NET Applications" and "Building an Enhanced Security System with a Web Cam and a Servo," I showed how to incorporate your web cam into your .NET applications and use it for surveillance purposes. However, using those solutions, the videos can only be viewed locally on the computer running the web cam. An interesting enhancement would be to extend the applications so that the video can be viewed remotely over the Internet.

And so, in this article I will show you how to use socket communication to send a live video image from a server to remote clients. The sample applications built in this article consists of:

  • A server that displays the video captured by a web cam
  • A client that receives the live video image from the server

The server will allow multiple clients to connect to it at the same time. Among other things, it is a useful application for the home environment where you can use it in your office to monitor your home or kids.

Creating the Server
I'll first create the server. Using Visual Studio 2005, create a new Windows application and name it RemoteMonitoring. In the default Form1, add a PictureBox control (see Figure 1) and set its properties as follows:

  • Size—449, 253
  • SizeMode—StretchImage

Figure 1. Add a PictureBox control to Form1.
Figure 2. You can now preview the video from the web cam in the application.

Switch to the code-behind of Form1 and import the following namespace:

Imports System.Runtime.InteropServices

Declare the constants and variable used for displaying the web cam image:

Public Class Form1 '---constants for capturing the video from webcam--- Const WM_CAP_START = &H400S Const WS_CHILD = &H40000000 Const WS_VISIBLE = &H10000000 Const WM_CAP_DRIVER_CONNECT = WM_CAP_START + 10 Const WM_CAP_DRIVER_DISCONNECT = WM_CAP_START + 11 Const WM_CAP_EDIT_COPY = WM_CAP_START + 30 Const WM_CAP_SEQUENCE = WM_CAP_START + 62 Const WM_CAP_FILE_SAVEAS = WM_CAP_START + 23 Const WM_CAP_SET_SCALE = WM_CAP_START + 53 Const WM_CAP_SET_PREVIEWRATE = WM_CAP_START + 52 Const WM_CAP_SET_PREVIEW = WM_CAP_START + 50 Const SWP_NOMOVE = &H2S Const SWP_NOSIZE = 1 Const SWP_NOZORDER = &H4S Const HWND_BOTTOM = 1 '---used as a window handle--- Dim hWnd As Integer

After declaring the constants, you need to declare the functions for video capture. The first two functions are needed for video capturing purposes and can be found in the avicap32.dll library, while the next three functions (found in user32.dll) are used for manipulating the various windows.

'--The capGetDriverDescription function retrieves the version ' description of the capture driver-- Declare Function capGetDriverDescriptionA Lib "avicap32.dll" _ (ByVal wDriverIndex As Short, _ ByVal lpszName As String, ByVal cbName As Integer, _ ByVal lpszVer As String, _ ByVal cbVer As Integer) As Boolean '--The capCreateCaptureWindow function creates a capture window-- Declare Function capCreateCaptureWindowA Lib "avicap32.dll" _ (ByVal lpszWindowName As String, ByVal dwStyle As Integer, _ ByVal x As Integer, ByVal y As Integer, _ ByVal nWidth As Integer, _ ByVal nHeight As Short, ByVal hWnd As Integer, _ ByVal nID As Integer) As Integer '--This function sends the specified message to a window or ' windows-- Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Integer, ByVal Msg As Integer, _ ByVal wParam As Integer, _ <MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) _ As Integer '--Sets the position of the window relative to the screen buffer-- Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _ (ByVal hwnd As Integer, _ ByVal hWndInsertAfter As Integer, ByVal x As Integer, _ ByVal y As Integer, _ ByVal cx As Integer, ByVal cy As Integer, _ ByVal wFlags As Integer) As Integer '--This function destroys the specified window-- Declare Function DestroyWindow Lib "user32" _ (ByVal hndw As Integer) As Boolean

Define the PreviewVideo() subroutine so that you can display the image captured by the web cam in a PictureBox control:

'---preview the selected video source--- Private Sub PreviewVideo(ByVal pbCtrl As PictureBox) hWnd = capCreateCaptureWindowA(0, _ WS_VISIBLE Or WS_CHILD, 0, 0, 0, _ 0, pbCtrl.Handle.ToInt32, 0) If SendMessage( _ hWnd, WM_CAP_DRIVER_CONNECT, _ 0, 0) Then '---set the preview scale--- SendMessage(hWnd, WM_CAP_SET_SCALE, True, 0) '---set the preview rate (ms)--- SendMessage(hWnd, WM_CAP_SET_PREVIEWRATE, 30, 0) '---start previewing the image--- SendMessage(hWnd, WM_CAP_SET_PREVIEW, True, 0) '---resize window to fit in PictureBox control--- SetWindowPos(hWnd, HWND_BOTTOM, 0, 0, _ pbCtrl.Width, pbCtrl.Height, _ SWP_NOMOVE Or SWP_NOZORDER) Else '--error connecting to video source--- DestroyWindow(hWnd) End If End Sub

You can now test if you are able to preview the video by calling the PreviewVideo() subroutine in the Form1_Load event:

Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load PreviewVideo(PictureBox1) End Sub

Press F5 to test the application (be sure to connect your web cam to your computer now). Figure 2 shows the webcam video displayed in the PictureBox control.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



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