Selecting TWAIN Device from a Customised List with csXImage

When a scanner or webcam is used to acquire images in csXImage the SelectTwainDevice command is normally used so that the user can select the appropriate device on their system. This command works by requesting the TWAIN Source Manager to display a dialogue box which lists all the installed devices. The user then selects from this list.

In some situations, it may be preferable to only allow the user to select from a restricted list of devices. One example is when WIA drivers are present on the system. WIA drivers can be used through a TWAIN application, but they usually offer limited functionality and can cause instability in the application, so it might be better not to allow the user to select a WIA driver.

The device name of WIA drivers is always prefixed by the string "WIA-" so they can easily be identified. The following example code shows how to retrieve a list of device names and then populate a Combobox using only those devices that are not WIA. Other criteria for filtering the available devices could be used as required.

Example 1 - VB.NET

This code loops through the list of device names and populates a Combobox called cboSelect. Devices with names beginning with "WIA-" are excluded. It would normally be run when the form loads so is shown here in the fMain_Load procedure.

Private Sub fMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim N As Long
Dim i As Long

N = AxImageBox1.TwainDeviceCount

cboSelect.Items.Clear()
For i = 1 To N
  If AxImageBox1.get_TwainDeviceName(i - 1).Substring(0, 4) <> "WIA-" Then
    cboSelect.Items.Add(AxImageBox1.get_TwainDeviceName(i - 1))
  End If
Next i
cboSelect.SelectedIndex = 0

End Sub

When an image is to be acquired, the device is then selected using the SelectTwainDeviceByName method. The name is taken from the Text property of the Combobox as follows.

AxImageBox1.SelectTwainDeviceByName(cboSelect.Text)
AxImageBox1.Acquire()

More VB.NET examples.

Example 2 - C#

The equivalent code sequences using C# is shown below.

private void Form1_Load(object sender, EventArgs e)
{
  int n = axImageBox1.TwainDeviceCount;
  comboBox1.Items.Clear();
  for (int i = 1; i <= n; i++)
  {
    if (axImageBox1.get_TwainDeviceName(i - 1).Substring(0, 4) != "WIA-")
    {
      comboBox1.Items.Add(axImageBox1.get_TwainDeviceName(i - 1));
    }
  }
  comboBox1.SelectedIndex = 0;
}

When an image is to be acquired, the device is then selected using the SelectTwainDeviceByName method. The name is taken from the Text property of the Combobox as follows.

axImageBox1.SelectTwainDeviceByName(comboBox1.Text);
axImageBox1.Acquire();

More C# examples.

Example 3 - VB5/6

The equivalent code sequences using VB5 or 6 are shown below.

Private Sub Form_Load()

N = ImageBox1.TwainDeviceCount

cboSelect.Clear
For i = 1 To N
  If Left(ImageBox1.TwainDeviceName(i - 1), 4) <> "WIA-" Then
    cboSelect.AddItem ImageBox1.TwainDeviceName(i - 1)
  End If
Next i
cboSelect.ListIndex = 0

End Sub

When an image is to be acquired, the device is then selected using the SelectTwainDeviceByName method. The name is taken from the Text property of the Combobox as follows.

ImageBox1.SelectTwainDeviceByName cboSelect.Text
ImageBox1.Acquire

More VB 6 examples.

Example 4 - Javascript

To use the same method in a clientside web application is slightly more complicated. A drop-down list is constructed using Javascript and then written into the web page using document.write. The relevant code blocks are shown below, and a complete HTML page demonstrating the technique can be downloaded here.

<script language="JavaScript">
  var NDevices = csxi.TwainDeviceCount;
  var SelectStr = '<select name=\"selectscanner\">\n'
  for(var i = 0; i<NDevices; i++)
  {
    if (csxi.TwainDeviceName(i).substr(0, 4) != 'WIA-')
    {
      SelectStr += '<option value=\"'
      SelectStr += csxi.TwainDeviceName(i)
      SelectStr += '\"'
      SelectStr += '>'
      SelectStr += csxi.TwainDeviceName(i)
      SelectStr += '</option>\n'
    }
  }
  SelectStr += '</select>\n'
  document.write(SelectStr)
</script>

 

csxi.SelectTwainDeviceByName(document.form1.selectscanner.value);
csxi.Acquire();

More Javascript examples.

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.