Using a Twain Scanner from VB.NET

We have an example project for VB.NET that shows how to use the csXImage ActiveX control to acquire an image from a Twain enabled scanner. Download the project code.

The demo uses the trial version of csXImage, which must must be registered on the development computer and the licence file must be in the same folder as the .ocx control. Running our installer will have performed the registration and unpacking of files.

When the VB form is loaded, and event runs to populate the combo boxes with the available Twain devices and pixel types. TwainDeviceCount returns the number of Twain devices installed on the system. TwainDeviceName returns the name of each device and the index of the device will be used later to set the CurrentTwainDevice property. This is an alternative approach to setting the device with the SelectTwainDevice method which uses a standard dialogue box from the Twain DLL to select the scanner.

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

  ' Initialise the contents of the various comboboxes

  N = AxImageBox1.TwainDeviceCount

  cboSelect.Items.Clear()
  For i = 1 To N
    cboSelect.Items.Add(AxImageBox1.get_TwainDeviceName(i - 1))
  Next i
  cboSelect.SelectedIndex = 0

  cboPixType.Items.Clear()
  cboPixType.Items.Add("Black/White")
  cboPixType.Items.Add("Greyscale")
  cboPixType.Items.Add("RGB Colour")
  cboPixType.SelectedIndex = 2

End Sub

Here is a screenshot of the application after the form has loaded. One or more devices have been found and the first is shown as an Epson scanner.

ActiveX Twain example for VB.NET

If the check box is ticked, the scanner's own interface will be used. The user can then set the page size and resolution using this interface. Alternatively, the box can be cleared and the scanner settings will be sent from csXImage. The middle part of the form allows the user to set the pixel type, resolution and page layout.

The code to acquire the image is quite simple. In this example, most of the code is involved with reading the form to set scanner properties if the default interface is not used. The important code for scanning is to set CurrentTwainDevice to select the scanner. Then call Acquire to scan or load the default interface. Once the scan is complete the image will be loaded into the csXImage control where further processing can take place or the image can be saved.

Private Sub cmdAcquire_Click(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles cmdAcquire.Click

  With AxImageBox1

    ' Select the device shown in the combobox
    .CurrentTwainDevice = cboSelect.SelectedIndex

    ' Only continue if the device is connected
    If .TwainConnected Then

      ' Either use the interface, or set properties from the
      ' various text boxes and the combobox
      If chkUseInt.Checked Then

        .UseTwainInterface = True

      Else

        .TwainPixelType = cboPixType.SelectedIndex
        If IsNumeric(txtRes.Text) Then
          .TwainResolution = Val(txtRes.Text)
        End If
        If IsNumeric(txtTop.Text) And IsNumeric(txtLeft.Text) And _
          IsNumeric(txtRight.Text) And IsNumeric(txtBtm.Text) Then
        .SetTwainLayout(Val(txtLeft.Text), Val(txtRight.Text), _
          Val(txtTop.Text), Val(txtBtm.Text))
        End If
        .UseTwainInterface = False

      End If

      ' Acquire the image
      .Acquire()

    End If

  End With

End Sub