Camera Minimal

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

  • install the EasyImage toolkit
  • create a camera client object that will capture frames for you as bitmaps
  • attach an event handler to the camera that delivers frames to you as bitmaps
  • display these frames on your screen (using delegates to be thread-safe) so you see it as live video
  • as part of the above, see how the camera server works and how you can activate it.

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.,

  • right click on References
  • select Add Reference from the popup menu
  • from the Browse tab, navigate to the EasyImage.dll and select it.

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.

  • PictureBox will display your camera's frames
    • Size = 320, 240 the default video frame size
    • SizeMode = AutoSize (the pictureBox will resize itself to fit the image)

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

  • Line 4 is needed to include the EasyImages package
  • Line 9 declares an EasyImage CameraClient object. Behind the scenes, this object will connect to a CameraServer program that actually does all the work of connecting to the camera (see below).
  • Line 10 creates a delegate which is used to display images in a pictureBox in a thread-safe manner. See the How-to: A Primer on Using Delegates to Display Images
  • Line 19 actually creates the Camera object
  • Line 20 declares an event handler that is invoked whenever the Camera Client captures a new frame. Since no frame rate has been specified, it will just use a default frame rate.
  • Line 21 starts the Camera Client. It assumes that you have already started the CameraServer (included as part of the EasyImages installation package and available from the start menu). However, if the program sees it that the server is not running, it will try to start it for you via a set of dialog boxes. At that point, it will begin capturing frames. See the next section for details.
  • Lines 24-27 is the event handler for the camera. It returns a bitmap containing the current frame. While we could try to display the bitmap in the picture box immediately, this is not thread-safe. That is, displaying an image in a picture box may fail occassionally due to threading issues. To resolve this, we call a routine that does this in a thread-safe way.
  • Lines 29-40 This function actually displays the bitmap image in the picture box. It checks to see what thread we are in; if its in the correct one, it assigns it. Otherwise it creates a delegate and invokes that instead. Note that this is the recommended way to do things like this in .NET, and is not something imposed by EasyImages. Again, see the How-to: A Primer on Using Delegates to Display Images