ImageGear .NET v24.12 - Updated
Acquiring Images from TWAIN Devices
User Guide > How to Work with... > Common Operations > Scanning > TWAIN Scanning > Acquiring Images from TWAIN Devices

TWAIN was developed by a consortium of software and hardware manufacturers to standardize the communications between application programs and image acquisition devices. The interface is optimized for the acquisition of graphics images. The TWAIN standard is widely supported by scanner manufacturers. In addition to scanners, TWAIN is being used with digital cameras and video capture boards. ImageGear supports all three TWAIN Transfer Modes: Native Data Transfer Mode (which is the TWAIN default), Disk File Transfer, and Buffered Memory.

A TWAIN-compliant image acquisition device is one whose device driver complies with the TWAIN specification. The device driver understands the TWAIN protocol, thus allowing interaction with the Data Source Manager, the main interface module of the TWAIN software. The Data Source Manager "manages the session" between the application program and the raster-generating data source. Three software elements work together in TWAIN: the application, the Data Source (DS), and the Data Source Manager (DSM) as shown in the illustration below: 

The following terminology is used to discuss ImageGear scanning:

The TWAIN Data Source for your device and the TWAIN Data Source Manager should be included as part of your scanner software. They are not part of the ImageGear software.

The process of acquiring an image (or set of images) from a device is as follows:

  1. Open the Data Source. This can be done automatically by the application (it chooses the Data Source for the end-user). Alternatively, the application can obtain a list of Data Sources from the Data Source Manager and let the end-user choose, or the application can have ImGearTWAIN Class display the list via its standard dialog box.
  2. Set the image acquisition parameters (single-page, multi-page, image size, etc.). This can be done automatically by the application, or the application can obtain the various Data Source capabilities (called "ScanCaps"), display them to the end-user in a dialog, and let the end-user choose the values. Alternatively, the application can let ImGearTWAIN Class obtain the settings from the end-user via its standard ScanCaps dialog. See ImGearCapabilities for a list of supported capabilities.
  3. Acquire the image(s). Each image is loaded into an ImGearPage Class object. For a multi-page acquisition, the ImGearPages are loaded into an ImGearDocument Class.
  4. Close the Data Source.

Thus, an example of the code needed to acquire an image from a TWAIN Data Source is as follows:

C#
Copy Code
using System;
using ImageGear.Core;
using ImageGear.TWAIN;

public ImGearPage AcquireImage(IntPtr Handle)
{
    ImGearPage igPage = null;
    ImGearTWAIN igTWAIN = new ImGearTWAIN();

    igTWAIN.WindowHandle = Handle;
    igTWAIN.UseUI = true;

    try
    {
        // Open the data source selection dialog
        igTWAIN.OpenSource(String.Empty);

        // Initialize the scanning
        igPage = igTWAIN.AcquireToPage();
    } 

    catch(ImGearException e)
    {
        // Handle the exception ...
    }

    finally
    {
        if(igTWAIN.DataSourceManagerOpen == true)
        {
            igTWAIN.CloseSource();
        }
    }

    return igPage;
}
VB.NET
Copy Code
Imports System
Imports ImageGear.Core
Imports ImageGear.TWAIN

Public Function AcquireImage(ByVal Handle As IntPtr) As ImGearPage
    Dim igPage As ImGearPage = Nothing
    Dim igTWAIN As ImGearTWAIN = New ImGearTWAIN()

    igTWAIN.WindowHandle = Handle
    igTWAIN.UseUI = True

    Try
        ' Open the data source selection dialog
        igTWAIN.OpenSource(String.Empty)

        ' Initialize the scanning
        igPage = igTWAIN.AcquireToPage()
    Catch e As ImGearException
        ' Handle the exception ...
    Finally
        If igTWAIN.DataSourceManagerOpen = True Then
            igTWAIN.CloseSource()
        End If
    End Try

    Return igPage
End Function