Using an ADF Scanner from Visual Basic (VB6)

We have an example project for VB 6 that shows how to use the csXImage ActiveX control to acquire multiple images from a Twain enabled scanner that has an auto document feeder (ADF). The project files can be downloaded.

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 to request the number of pages to scan. This makes it easier to follow than setting the values in the VB IDE.

Here is a screenshot of the application after the form has loaded. There are some simple options available that demonstrate some important techniques. Options inlcude whether to use the built in user interface supplied with the scanner and whether to use duplex scanning. The resulting scan is saved and options include separate TIFF files, a single multipage TIFF file, or separate multipage TIFF files where a blank separator page is used between documents.

Demo project using VB6 with an ADF Twain scanner

The working part of the code is in two sections. The button marked "Acquire" starts the scanning process. After each page is scanned an event is triggered by csXImage, the OnAcquire event, and this event handler contains the code that processes each image.

The SelectTwainDevice method calls a standard dialogue box from Twain to select an installed device from a list. The HasADF property is queried to see if the device actually has an ADF fitted and if not, the user is given the option to cancel or continue. The properties TwainMultiImage and UseADF are both set to true before scanning starts. The TwainImagesToRead property is set, either to zero if all the pages in the feeder are to be read, or to a fixed number of pages. The BlankTol property is used to define a blank page.

Other properties to set first, based on values returned from the form, are UseTwainInterface and TwainDuplexEnabled. The ClearTIF method is called so that any multipage TIFF already in memory is removed before creating another. The Acquire method starts the scan.

Private Sub cmdAcquire_Click()

Dim Cancel As Boolean Dim i As Integer

Cancel = False

With ImageBox1
  If .SelectTwainDevice Then
    If Not .HasADF Then
      If MsgBox("No ADF detected on this device. Continue?", _
        vbOKCancel) = vbCancel Then
        Cancel = True
      End If
    End If
  Else
    Cancel = True
  End If

  If Not Cancel Then
    .TwainMultiImage = True
    .UseADF = True
    If cboPages.Text = "Max." Then
      ' Continues acquiring images until ADF is empty
      .TwainImagesToRead = 0
    Else
      .TwainImagesToRead = cboPages.Text
    End If
    ' This value is the tolerance for defining a blank page.
    ' It is set to a high value to ensure this demo will
    ' work well even with colour scanning using sheets that are not pure white.
    ' For black and white scanning with plain white separator pages a lower value
    ' (e.g., 100) is appropriate.
    .BlankTol = 10000
    .ClearTIF
    .UseTwainInterface = chkUI.Value
    .TwainDuplexEnabled = chkDuplex.Value

    .Acquire

    If optFile(1).Value Then
      .WriteTIF txtFileName.Text & ".tif"
    End If

    If optFile(2).Value Then
      .WriteTIF txtFileName.Text & Trim(Str(FileNum)) & ".tif"
    End If

  End If

End With

End Sub

The OnAcquire event is triggered after each page is scanned and the event handler can be used to run code to process each image. The control is redrawn so that the current image is displayed. Then the image is either saved as an individual TIFF file or it is written into a multipage TIFF which is stored in memory until it is saved later. There is some conditional logic to deal with the different options selected by the user.

An important point is that when a multipage document is created, the Compression and ColorFormat properties are set before calling AddToTIF. These properties apply to each page, not to the entire document.

Private Sub ImageBox1_OnAcquire()

' Redraw the control to display the current image
ImageBox1.Redraw

If optFile(0).Value Then

  ' Save image as an individual file
  ImageBox1.SaveToFile txtFileName.Text & Trim(Str(FileNum)) & ".tif"
  FileNum = FileNum + 1

ElseIf optFile(1).Value Then

  ' Save multi-page TIFF file
  ' Use Group 4 compression for black & white images
  If ImageBox1.ColorFormat = cfMonoBW Then
    ImageBox1.Compression = cmGroup4
  End If
  ImageBox1.AddToTIF 0

Else

  If ImageBox1.IsBlank Then
    ImageBox1.WriteTIF txtFileName.Text & Trim(Str(FileNum)) & ".tif"
    FileNum = FileNum + 1
    ImageBox1.ClearTIF
  Else
    ImageBox1.AddToTIF 0
  End If

End If

End Sub