From iLab Cookbook

Toolkits: CameraMinimal

<< Back to the EasyImage Toolkit page

Tutorial - A Minimalist Camera

What you will learn

This tutorial illustrates how to use EasyImage's Camera. You will learn how to

Download

While we recommend you create this program from scratch, you can also download the source and executables.

Source and Executables of this and other examples: EasyImagesExamples.zip
  • Unzip all files into a single folder.
  • Try the executable; a shortcut is in the top-level directory, or look in the debug directory
  • You will need a web camera attached to your computer.

Step 1. Including EasyImages in your Visual Studio 2005 C# project

These steps are typical of how one includes a dll in VS 2005.

1) Open Visual Studio .NET and create a new C# Windows Application. Call it something like CameraExample.

2) From the EasyImages download page, download and copy the EasyImage.dll into your project folder (available from the EasyImage home page). Note that a copy of this dll is included in the tutorial examples in the folder ''EasyImagesDll'

3) In the Solution Explorer, add a reference to EasyImage.dll, i.e.,

4) In the code view of your form (usually Form1.cs) and in the using section at the top, type using EasyImages; (see Line 3 in the example code below).

Step 2. Creating GUI controls

5). Add a PictureBox to your form window, so it looks like the window at the top of this page.

Step 3. Writing the program

The entire program is listed below. Enter it, and try it. Then read the explanation that follows.

1  using System;
2  using System.Windows.Forms;
3  using System.Drawing;
4  using EasyImages;
5  namespace CameraMinimal
6  {
7    public partial class Form1 : Form
8    {
9      private EasyImages.CameraClient camera;
10     private delegate void SetPictureBoxImage(PictureBox pbox, Bitmap image);
11
12     public Form1()
13     {
14       InitializeComponent();
15     }
16
17     private void Form1_Load(object sender, EventArgs e)
18     {
19       camera = new EasyImages.CameraClient("DefaultCamera");
20       camera.ReceivedFrame += new CameraClient.ReceivedFrameEventHandler(camera_ReceivedFrame);
21       camera.Start();
22     }
23
24     private void camera_ReceivedFrame(object sender, CameraEventArgs e)
25     {
26       this.DisplayImageInPictureBox(pictureBox1, e.Bitmap);
27     }
26
29     private void DisplayImageInPictureBox(PictureBox pbox, Image image)
30     {
31       if (pbox.InvokeRequired) // We are in the wrong thread. Call ourselves in the correct thread
32       {
33         SetPictureBoxImage theDelegate = new SetPictureBoxImage(DisplayImageInPictureBox);
34         BeginInvoke(theDelegate, new object[] { pbox, image })
35       }
36       else // we are in the correct thread, so assign the image
37       {
38         pbox.Image = image;
39       }
40     }
41   }
42 }

The explanation below only describes the parts of the program that are unique to using EasyImage's camera

Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/CameraMinimal
Page last modified on July 16, 2007, at 09:44 PM