Using a Twain Scanner from Visual Basic (VB6)

We have an example project for VB 6 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 Form_Load()

Dim N As Long
Dim i As Long

' Initialise the contents of the various comboboxes

N = ImageBox1.TwainDeviceCount

cboSelect.Clear
For i = 1 To N
  cboSelect.AddItem ImageBox1.TwainDeviceName(i - 1)
Next i
cboSelect.ListIndex = 0

cboPixType.Clear
cboPixType.AddItem "Black/White"
cboPixType.AddItem "Greyscale"
cboPixType.AddItem "RGB Colour"
cboPixType.ListIndex = 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 VB6

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

With ImageBox1
  ' Select the device shown in the combobox
  .CurrentTwainDevice = cboSelect.ListIndex

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

    ' Either use the interface, or set properties from the
    ' various text boxes and the combobox

    If chkUseInt.Value Then

      .UseTwainInterface = True

    Else

      .TwainPixelType = cboPixType.ListIndex
      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

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.