Using an ADF Scanner from VB.NET

We have an example project for VB.NET 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 form is loaded an 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 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 VB.NET 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(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles cmdAcquire.Click

  Dim Cancel As Boolean

  Cancel = False

  With AxImageBox1
    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.Checked
      .TwainDuplexEnabled = chkDuplex.Checked

      .Acquire()

      If rdbMulti.Checked Then
        .WriteTIF(txtFileName.Text & ".tif")
      End If

      If rdbSep.Checked 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 AxImageBox1_OnAcquire(ByVal sender As System.Object, ByVal e
    As System.EventArgs) Handles AxImageBox1.OnAcquire

    ' Redraw the control to display the current image
    AxImageBox1.Redraw()

    If rdbSingle.Checked Then

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

    ElseIf rdbMulti.Checked Then

      ' Save multi-page TIFF file
      ' Use Group 4 compression for black and white images
      If AxImageBox1.ColorFormat = csXImageTrial.TxColorFormat.cfMonoBW Then
        AxImageBox1.Compression = csXImageTrial.TxCompression.cmGroup4
      End If
      AxImageBox1.AddToTIF(0)

    Else

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

  End If

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.