C#

<< Click to Display Table of Contents >>

Navigation:  Programming with Common Vision Blox > Hello CVB >

C#

 

1.Start the Visual Studio development environment and choose New from the File menu.
On the Project types dialog select the C# programming language and create a new Windows Forms Application.
Enter CSNet_FirstCVBApplication as the project name:
 
CS_NewProject

 

2.   Confirm your input by clicking OK.

 

3. If this has not yet been done, it is now necessary to add the Common Vision Blox ActiveX components to the Visual Studio Toolbox.

   This step is only necessary the first time you use these components.

   From the Tools menu choose Choose Toolbox Items (also accessible through the tool box context menu).

   This will open the Customize Toolbox dialog box with a list of all registered ActiveX Components will appear:

 
CustomizeToolbox

 
Check all the components you want to add to the Toolbox
(the Common Vision Blox components' naming pattern is Common Vision ... Control, for the rest of this section you will at least need Display and Image Control) and press OK.
This will persistently add the selected components to the Toolbox - next time you create a new project with Visual Studio the Common Vision Blox controls will already be available in Visual Studio's Toolbox.

 
Toolbox
 

4.   Add one instance of the Image Control and one Instance of the Display Control to the form by selecting the respective control in the Toolbox and dropping it on the form.

 
CS_Workspace
 

5.   It is possible to specify an image to be loaded immediately when the ActiveX Control is being instantiated.

     To do this, open the control's general property page and click the Browse button to select an image or driver file.

     For the moment, use the file Clara.bmp from the %CVB%\Tutorial directory.

 
DotNet_CVImage_PPage
 

6.   You now have a fully functional Windows Forms application that contains a Common Vision Blox Display and Image Control.

      The Image Control even loads an image when the application starts - all that is required to make this image visible now is to establish a connection between the Display and the Image Control.
      This is done by copying the handle value in the Image Control's Image handle to the Display Control's Image handle (see description of ActiveX Controls).
      To do that, double click on the form's background.
      This will insert a new handler function for the Form_Load event and open it in the source code editor.
     Modify the event handler to establish the connection between Display and Image Control.
 
   private void Form1_Load(object sender, EventArgs e)
   {
       // set image to display
       axCVdisplay1.Image = axCVimage1.Image;
   }

 

7.   Compile and run the application.

  It should come up with a Common Vision Blox display that shows the bitmap from the file Clara.bmp:

 
DotNet_Application

 

8.   To take the application one step further, we now add the capability for displaying live images.
      First of all, instead of loading Clara.bmp, the program now needs to load a video interface driver, for example the GenICam.vin (make sure the driver is configured correctly with
      the Device Configurator; if you want to use a different vin driver simply substitute its name in the code below).
      To do that, simply add another line before the one that has been added in step 6:
 
   axCVimage1.LoadImage(@"%CVB%\Drivers\GenICam.vin");
   axCVdisplay1.Image = axCVimage1.Image;
 

9.   To be able to start and stop live acquisition, add a check box to the dialog...

 
CS_Form_Grab
 
... change its Text property from "Checkbox1" to "Grab", the Name from Checkbox1 to CheckBoxGrab and then double-click the check box.
This will insert an event handler for the control's default event (CheckedChanged) to your program and open it in the source code editor.
 
   private void CheckBoxGrab_CheckedChanged(object sender, EventArgs e)
   {
       // set grab status
       axCVimage1.Grab = CheckBoxGrab.Checked;
   }
 

10.   The last thing to implement now is an adequate reaction to the acquisition of a new frame.
        The Image Control reports newly acquired images by means of the ImageSnapped event that will be raised every time the camera has delivered a new image.
        To react to the ImageSnapped event select the Image Control on your form, then change the Properties Window to show the control's events.

        Simply double click the empty cell next to ImageSnaped to generate a new handler for the ImageSnapped event.

 
DotNet_Add_ImageSnaped_Event
 
In the new event handler's source simply add a call to the Display Control's Refresh method:
 
   private void axCVimage1_ImageSnaped(object sender, EventArgs e)
   {
       // refresh display
       axCVdisplay1.Refresh();
   }
 

11.  Now compile and run the application.

       You can:

start/stop live acquisition by checking/unchecking the Grab check box and the

Display will show the live image from the camera.

It is also directly possible to use the interactive zoom of the Display Control.

 
DotNet_Application_Live

 

Refer further code examples in the CVB Tutorials (%CVB%Tutorial folder).