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.


Teach Your Old Web Cam New Tricks: Use Video Captures in Your .NET Applications : Page 2

Just when you thought there wasn't anything new that you could do with a webcam, along comes this article, which shows you how to exploit the AVICap Windows class so you can integrate on-demand screen captures into your applications.




Application Security Testing: An Integral Part of DevOps

Using Video Capability in a Business Application
Now that I have managed to integrate video into my Windows application, I'll consider a practical use of it. In this section, I will use data-binding to display some records from the Employees table in the Northwind database (one of the sample databases that comes with SQL Server 2000).

If you are familiar with the Northwind database, you know that in the Employees table there is a field known as Photo. This field is used to store employees’ photographs. In a real-life application, it would be much easier to add in an employee’s photo if you can take a snapshot of him using a webcam and then directly add the record to the database. In this section, I will show you how to do that.

First, add a new data source to the project. You can do so using the Data->Add New Data Source… menu (see Figure 5).

Figure 5. Adding a Source: Use the simple menu command shown to launch the Data Source Configuration Wizard and add a new data source to the project.
Figure 6. Adding a Database Connection: You need to tell Visual Studio which new data source you wish to add, in this case, a connection to the Northwind database.

The Data Source Configuration Wizard will appear. Choose Database as the data source and click Next. In the next window, click the New Connection… button to establish a connection to the Northwind database.

Enter the name of the SQL Server containing the Northwind database. For my example, it is located on the same machine running Visual Studio 2005 and hence I entered "(local)." Select the Northwind database and then click OK (see Figure 6). Click Next when you are back to the previous window.

In the next window, you will choose the tables to use. Check the fields as shown in Figure 7. Click Finish to complete the process. The fields selected are: EmployeeID, LastName, FirstName, Title, and Photo. The Photo field is of the image data type, which contains images of employee. You will bind the Photo field to a PictureBox control on the windows form.

Figure 7. Fields and Tables: Each field corresponds to a table in the Northwind database. Here I've chosen five fields that provide data about an Employee, inclulding the Photo field, which will beind to a PictureBox control.
Figure 8. Setting the Binding: The screen shot shows the steps to change the default bindings of the Employees table so that it binds to a photo, which displays in a PictureBox control.

The Employees item will now appear in the Data Sources window (Data->Show Data Sources). Change the bindings of the Employees table as shown in Figure 8. Essentially, you want to view the Employees records in the detail view and bind the Photo field to a PictureBox control.

The Employees table should now look like Figure 9.

Figure 9. Bound to the Employees: The Employees table now shows up in the Data Sources window.
Figure 10. Data-binding in Action: Form 1 is automatically populated with data fields from the Employees table when the latter is dropped onto the form.

Drag the Employees table from the Data Sources window and drop it onto Form1 (see Figure 10). You should see that a few controls are added automatically to the form. Resize the PictureBox control and set its SizeMode property to AutoSize.

If you test the application now (by pressing F5), you will be able to view the records in the Employees table (see Figure 11.). You can add a record by clicking the Add New button, entering the value for each field, and then clicking the Save Data button to save the changes.

Figure 11. Employee View: You can now build the application and use it to view the records from the Employees table.
Figure 12. Adding a Button Control: The button you add will allow end users to capture their own photo and save it to the employee database.

This is where things get interesting. When you add a new record, you would want to make use of the webcam to take a photo of the employee and then save the photo directly to the database. To do so, first add a Button control to the bottom of the PictureBox control and name it Capture Photo (see Figure 12).

Double-click the Add New button so that when the user adds a new record, the video source is now bound to the PhotoPictureBox control:

Private Sub BindingNavigatorAddNewItem_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles BindingNavigatorAddNewItem.Click StopPreviewWindow() PreviewVideo(PhotoPictureBox) End Sub

When the user clicks on the Capture Photo button, you will call the CaptureImage subroutine to freeze the photo:

Private Sub btnCapturePhoto_Click( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnCapturePhoto.Click CaptureImage() End Sub

The CaptureImage() subroutine copies the image from the video source onto the clipboard. You then display the image using the PhotoPictureBox control:

'---save the image--- Private Sub CaptureImage() Dim data As IDataObject Dim bmap As Image '---copy the image to the clipboard--- SendMessage(hWnd, WM_CAP_EDIT_COPY, 0, 0) '---retrieve the image from clipboard and convert it ' to the bitmap format data = Clipboard.GetDataObject() If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then bmap = _ CType(data.GetData(GetType(System.Drawing.Bitmap)), _ Image) PhotoPictureBox.Image = bmap StopPreviewWindow() End If End Sub

Figure 13. Adding the Author: In this screen shot I'm taking a photo of myself to create a new record in the employee database.
That's it! Press F5 to test the application. When the form loads, click on the Add New button to add a new record. Enter the details as shown in Figure 13 and then position the webcam to take a nice snapshot of yourself. Smile and then click the Capture Photo button.

Finally, click the Save Data button to save the new record onto the database. You can verify that the photo is saved by restarting the application and then viewing the records again.

In this article, you have seen how to use the AVICap class to integrate video into your Windows application. While I have only showed you one particular usage, there are in fact many uses for video: you can build a video surveillance system using the techniques described in this article or you can build your own video conferencing application. The possibilities are endless and please let me know if you have an innovative application built using the techniques described in this article.

Wei-Meng Lee is a Microsoft MVP and founder of Developer Learning Solutions, a technology company specializing in hands-on training on the latest Microsoft technologies. He is an established developer and trainer specializing in .NET and wireless technologies. Wei-Meng speaks regularly at international conferences and has authored and coauthored numerous books on .NET, XML, and wireless technologies. He writes extensively on topics ranging from .NET to Mac OS X. He is also the author of the .NET Compact Framework Pocket Guide, ASP.NET 2.0: A Developer's Notebook (both from O'Reilly Media, Inc.), and Programming Sudoku (Apress). Here is Wei-Meng's blog.
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