IC Imaging Control .NET

Making Device Settings

Making Device Settings

This chapter shows you how to create a custom device settings dialog. The code is provided in reusable form, thus it can be easily integrated into your own applications.

IC Imaging Control also offers a built-in dialog, which can be used to select and set up a device. If, however, the dialog provided by IC Imaging Control does not fit your needs, this chapter shows you how to create a custom dialog. It will first show you how to integrate the sample code into your own projects and then how to actually build the dialog.

Integrating the sample code into your project

The source code for the VB.NET and C# versions of this sample program can be found in the directories samples\VB *\Making Device Settings and samples\C# *\Making Device Settings in your My Documents/IC Imaging Control 3.4 directory. To integrate the code into your project, you just have to add the file frmDeviceSettings.vb or frmDeviceSettings.cs. Select Project from the menu and then select Add Existing Item.... Now browse to the sample folder and open the file frmDeviceSettings.vb or frmDeviceSettings.cs.

To call the dialog from your application, you will need an instance of the dialog first. After the instance is created, assign the ICImagingControl variable (here: IcImagingControl1, or icImagingControl1 if you are using C#) to the dialog and then show the dialog. After this, Dispose should be called for the dialog:

[VB.NET]
Dim DeviceDialog As New frmDeviceSettings DeviceDialog.ImagingControl = IcImagingControl1 DeviceDialog.ShowDialog() DeviceDialog.Dispose()
[C#]
using( frmDeviceSettings DeviceDialog = new frmDeviceSettings() ) { DeviceDialog.ImagingControl = icImagingControl1; DeviceDialog.ShowDialog(); }

Creating the Dialog

In this chapter, we will focus on the device settings dialog. In addition to the dialog, you will of course need an application that calls it. The sample code of this chapter contains a ready-to-run project with a small application that calls the dialog.

Assuming you have already set up a main project, you now have to add a new form to the project. This form will contain the elements of the custom dialog. To add a new form, select Project from the menu and then choose Add Windows Form. Name it frmDeviceSettings and click the Open button.

Now add 5 combo boxes to the form. Name them cboDevice, cboVideoNorm, cboVideoFormat, cboFrameRate and cboInputChannel. Also add a text box, called txtSerial and two check boxes named chkFlipV and chkFlipH.

In addition to the controls mentioned above, the sample dialog also contains a label named lblErrorMessage. The Visible property of this label is set to false, so that it will not be displayed when the dialog is called. The reason for this is explained when the Form_Load event is discussed.

image

Adding Functionality to the Dialog

First add some global variables:

[VB.NET]
Public ImagingControl As TIS.Imaging.ICImagingControl Private DeviceState As String Const NOT_AVAILABLE = "n/a"
[C#]
public TIS.Imaging.ICImagingControl ImagingControl; private string DeviceState; private const string NOT_AVAILABLE = "n\a";

ImagingControl must be public since it is assigned from the main program before the dialog is called.

Now add a Form_Load event and insert the following code:

[VB.NET]
Private Sub frmDeviceSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load If ImagingControl.DeviceValid Then If ImagingControl.LiveVideoRunning Then lblErrorMessage.Text = "The device settings dialog is not available while the live video is running.\n\nStop the live video first." lblErrorMessage.AutoSize = False lblErrorMessage.Padding = New Padding(8) lblErrorMessage.SetBounds(0, 0, 100, cmdOK.Top) lblErrorMessage.Dock = DockStyle.Top lblErrorMessage.Visible = True Exit Sub Else lblErrorMessage.Visible = False End If End If SaveDeviceSettings() UpdateDevices() End Sub
[C#]
private void frmDeviceSettings_Load( object sender, EventArgs e ) { if( ImagingControl.DeviceValid ) { if( ImagingControl.LiveVideoRunning ) { lblErrorMessage.Text = "The device settings dialog is not available while the live video is running.\n\nStop the live video first."; lblErrorMessage.AutoSize = false; lblErrorMessage.Padding = new Padding( 8 ); lblErrorMessage.SetBounds( 0, 0, 100, cmdOK.Top ); lblErrorMessage.Dock = DockStyle.Top; lblErrorMessage.Visible = true; return; } else { lblErrorMessage.Visible = false; } } SaveDeviceSettings(); UpdateDevices(); }

The Form_Load event checks, whether the live video is running. If it is, it reformats the dialog box to display a message to tell you that the dialog will not be displayed, while the live video is running. This behavior was implemented to point out that changing the device will cause side effects. The ring buffer collection ICImagingControl.ImageBuffers is reset when the device is changed. Therefore, image data in the collection is lost.

Now add the click events of the OK and the Cancel button and insert the following code accordingly:

[VB.NET]
Private Sub cmdOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdOK.Click Me.Close() End Sub
[C#]
private void cmdOK_Click( object sender, System.EventArgs e ) { this.Close(); }
[VB.NET]
Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdCancel.Click RestoreDeviceSettings() Me.Close() End Sub
[C#]
private void cmdCancel_Click( object sender, System.EventArgs e ) { RestoreDeviceSettings(); this.Close(); }

As can be seen above, the Form_Load event calls a procedure SaveDeviceSettings and the CancelButton_Click event calls RestoreDeviceSettings.

The UpdateDevices procedure that is called on the Form_Load event updates the controls of the dialog, but this will be discussed later.

Now add the SelectedIndexChanged events for all 5 combo boxes and the click event for the 2 check boxes. On the SelectedIndexChanged event of the device combo box, the currently selected device must be changed and all other combo boxes, the check boxes and the output of the serial number text field must be updated. This is done with the following code:

[VB.NET]
Private Sub cboDevice_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDevice.SelectedIndexChanged Dim Serial As String = "" Try 'Open the device If cboDevice.Enabled Then Dim Item As Object Dim index As Integer index = 0 ImagingControl.Device = cboDevice.Text txtSerial.Text = NOT_AVAILABLE For Each Item In ImagingControl.Devices If Item.Name = cboDevice.Text Then If ImagingControl.Devices(index).GetSerialNumber(Serial) Then txtSerial.Text = Serial Exit For End If End If index = index + 1 Next ' Get supported video norms, formats and inputs UpdateVideoNorms() UpdateInputChannels() UpdateFlip() End If Catch ex As System.Exception MessageBox.Show(ex.Message) End Try End Sub
[C#]
private void cboDevice_SelectedIndexChanged( object sender, System.EventArgs e ) { string Serial = ""; try { // Open the device if( cboDevice.Enabled ) { int index = 0; ImagingControl.Device = cboDevice.Text; txtSerial.Text = NOT_AVAILABLE; foreach( object Item in ImagingControl.Devices ) { if( Item.ToString() == cboDevice.Text ) { if( ImagingControl.Devices[index].GetSerialNumber( out Serial ) ) { txtSerial.Text = Serial; break; } } index++; } } // Get supported video norms, formats and inputs UpdateVideoNorms(); UpdateInputChannels(); UpdateFlip(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } }

After the new device is selected, its serial number is retrieved. If a serial number is not available, n/a is displayed. The procedures UpdateVideoNorms, UpdateInputChannels and UpdateFlip update the controls. They will be discussed later. Please note that the video format and the frame rate are updated implicitly by UpdateVideoNorms and UpdateVideoFormat.

The SelectedIndexChanged events for the other combo boxes are quite similar. The SelectedIndexChanged event for the cboVideoNorm combo box includes a procedure UpdateVideoFormats, which is used to update the video format combo box according to the selected video norm. The SelectedIndexChanged event for the cboVideoFormat combo box includes a procedure UpdateFrameRates that updates the frame rate combo box after the video format has changed. The other combo boxes just set the currently selected value. Here is the code for the video norm combo box:

[VB.NET]
Private Sub cboVideoNorm_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboVideoNorm.SelectedIndexChanged Try If cboVideoNorm.Enabled Then ImagingControl.VideoNorm = cboVideoNorm.Text End If UpdateVideoFormats() Catch ex As System.Exception MessageBox.Show(ex.Message) End Try End Sub
[C#]
private void cboVideoNorm_SelectedIndexChanged( object sender, System.EventArgs e ) { try { if( cboVideoNorm.Enabled ) { ImagingControl.VideoNorm = cboVideoNorm.Text; } UpdateVideoFormats(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } }

The SelectedIndexChanged events for the other combo boxes are implemented accordingly, except a minor detail regarding the frame rate combo box. If you assign the new value for the frame rate, make sure you do a typecast, since cboFrameRate.Text returns a string, but ICImagingControl.DeviceFrameRate needs a floating point value.

Finally, add click events for the check boxes. They just toggle the horizontal and vertical flip:

[VB.NET]
Private Sub chkFlipV_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkFlipV.Click If ImagingControl.DeviceFlipVerticalAvailable Then ImagingControl.DeviceFlipVertical = (chkFlipV.Checked = True) End If End Sub
[C#]
private void chkFlipV_CheckedChanged( object sender, System.EventArgs e ) { if( ImagingControl.DeviceFlipVerticalAvailable ) { ImagingControl.DeviceFlipVertical = ( chkFlipV.Checked == true ); } }

The event for chkFlipH is implemented the same way.

The Update Procedures

As mentioned before, there is an Update procedure for every combo box and one for the two check boxes. The Update procedures for the combo boxes just fill the appropriate combo box with currently valid values. If the value that was selected before the update is still contained in the combo box, it is selected automatically. The code for the UpdateDevice procedure looks as follows:

[VB.NET]
Private Sub UpdateDevices() cboDevice.Items.Clear() If ImagingControl.Devices.Length > 0 Then Dim Item As Object For Each Item In ImagingControl.Devices cboDevice.Items.Add(Item.ToString()) Next If ImagingControl.DeviceValid Then cboDevice.SelectedItem = ImagingControl.Device Else cboDevice.SelectedIndex = 0 End If cboDevice.Enabled = True Else cboDevice.Items.Add(NOT_AVAILABLE) cboDevice.Enabled = False cboDevice.SelectedIndex = 0 End If End Sub
[C#]
private void UpdateDevices() { cboDevice.Items.Clear(); if( ImagingControl.Devices.Length > 0 ) { foreach( object Item in ImagingControl.Devices ) { cboDevice.Items.Add( Item.ToString() ); } if( ImagingControl.DeviceValid ) { cboDevice.SelectedItem = ImagingControl.Device; } else { cboDevice.SelectedIndex = 0; } cboDevice.Enabled = true; } else { cboDevice.Items.Add( NOT_AVAILABLE ); cboDevice.Enabled = false; cboDevice.SelectedIndex = 0; } }

The Update procedures for the other combo boxes are implemented accordingly, but note that ImagingControl.DeviceFrameRates returns a collection that contains floating point values. Therefore, in the UpdateFrameRates procedure, you must typecast them into a string before you can add them to the combo box.

The UpdateFlip procedure checks whether there is a vertical or horizontal flip available with the current device and sets their current state if possible:

[VB.NET]
Private Sub UpdateFlip() If ImagingControl.DeviceFlipHorizontalAvailable Then chkFlipH.Enabled = True If ImagingControl.DeviceFlipHorizontal Then chkFlipH.Checked = True Else chkFlipH.Checked = False End If Else chkFlipH.Enabled = False chkFlipH.Checked = False End If If ImagingControl.DeviceFlipVerticalAvailable Then chkFlipV.Enabled = True If ImagingControl.DeviceFlipVertical Then chkFlipV.Checked = True Else chkFlipV.Checked = False End If Else chkFlipV.Enabled = False chkFlipV.Checked = False End If End Sub
[C#]
private void UpdateFlip() { if( ImagingControl.DeviceFlipHorizontalAvailable ) { chkFlipH.Enabled = true; if( ImagingControl.DeviceFlipHorizontal ) { chkFlipH.Checked = true; } else { chkFlipH.Checked = false; } } else { chkFlipH.Enabled = false; chkFlipH.Checked = false; } if( ImagingControl.DeviceFlipVerticalAvailable ) { chkFlipV.Enabled = true; if( ImagingControl.DeviceFlipVertical ) { chkFlipV.Checked = true; } else { chkFlipV.Checked = false; } } else { chkFlipV.Enabled = false; chkFlipV.Checked = false; } } //> // ------------------------------------------------------------------------------ // UI events // ------------------------------------------------------------------------------ // // cboDevice_SelectedIndexChanged // // Get available inputs and video formats for the selected // device and enter the information in the respective combo // boxes. // //<<cboDevice private void cboDevice_SelectedIndexChanged( object sender, System.EventArgs e ) { string Serial = ""; try { // Open the device if( cboDevice.Enabled ) { int index = 0; ImagingControl.Device = cboDevice.Text; txtSerial.Text = NOT_AVAILABLE; foreach( object Item in ImagingControl.Devices ) { if( Item.ToString() == cboDevice.Text ) { if( ImagingControl.Devices[index].GetSerialNumber( out Serial ) ) { txtSerial.Text = Serial; break; } } index++; } } // Get supported video norms, formats and inputs UpdateVideoNorms(); UpdateInputChannels(); UpdateFlip(); } catch( Exception ex ) { MessageBox.Show( ex.Message ); } }

<< Programmer's Guide

Get in touch with us


About The Imaging Source

Established in 1990, The Imaging Source is one of the leading manufacturers of industrial cameras, frame grabbers and video converters for production automation, quality assurance, logistics, medicine, science and security.

Our comprehensive range of cameras with USB 3.1, USB 3.0, USB 2.0, GigE interfaces and other innovative machine vision products are renowned for their high quality and ability to meet the performance requirements of demanding applications.

Automated Imaging Association ISO 9001:2015 certified

Contact us