What Is TWAIN?

TWAIN is an industry standard software interface used to transfer still images into applications from devices, on Windows and Apple computers. TWAIN is not an acronym, but people insist on trying to make it into one.

The TWAIN standard is published, maintained, promoted and supported by the TWAIN Working Group - see www.twain.org.

TWAIN is not a hardware-level protocol.  It requires a driver (mysteriously called a Data Source or DS) for each imaging device.  When a device is advertised as TWAIN Compatible this simply means the device has a TWAIN DS (driver) available for it.

TWAIN is available at this time (March 2007) on all 32-bit Intel versions of Microsoft Windows and on Apple OS X. Note that Dosadi does not support Apple products or OS X development.

There is no TWAIN support, and no way to use TWAIN-compatible devices or software, on other platforms or on bare hardware.  On Linux, the closest existing standard is SANE: http://www.sane-project.org

TWAIN diagram.GIF (7093 bytes)Notes (the following applies only to Win32 platforms)

The TWAIN Manager or DSM is provided for free by the TWAIN Working Group - it acts as liason and coordinator between TWAIN applications and Data Sources.

The DSM has minimal user interface - just the Select Source dialog.  All user interaction outside of the Application is handled by the Data Source.

Each Data Source is basically a high-level device driver, provided by the device manufacturer. The TWAIN Working Group does not enforce compliance with the TWAIN standard. TWAIN compliance and compliance testing are voluntary, and left up to application and DS developers.

The TWAIN manager and the Data Source are DLLs, so they load into the application's memory space and run essentially as subroutines of the application.  The DSM uses interprocess communication to coordinate with other instances of itself when more than one application uses TWAIN.

Much simplified, the steps of an application using TWAIN are:

1. Open a device and have a conversation with the device (actually the DS) about the capabilities and settings of the device.  There is a huge list of capabilities, every device implements a different set.  But for example, most devices can list the resolutions they support, and most will allow an application to select a resolution for subsequent images.

2. Enable a device -which in TWAIN terms means 'give the device permission to deliver images.'  Most scanners when enabled display their user interface and allow the user to adjust scan settings, eventually hitting a scan button to actually start the scan.

The application can request that a DS not display a user interface, but some devices ignore the request and show their dialog anyway.  This is a common cause of surprise and frustration for TWAIN application developers.

3. Once a device is enabled, the application waits for a notification from the DS that an image is ready.  While it waits, the application must somehow ensure that any messages posted to it are routed through TWAIN.  Unless this is done correctly, the application will never receive the image-ready notification, and the device's user interface may not work correctly.

4. The application accepts the image from the DS.

TWAIN defines three modes of image transfer:
    Native - on Windows this is a DIB (Device Independent Bitmap) in memory.  Note that Windows has two kinds of bitmaps!
    Memory - strips or blocks of pixels in a series of memory buffers.
    File - the DS writes the image out directly to a file.   [A DS is not required to support this mode.]

5. If appropriate, the application may transfer multiple images until it chooses to stop, or until the DS signals that no more images are available.

6. The application normally then disables the DS and closes it - in symmetry with the open/enable steps we started with.


EZTwain sits between the application and the TWAIN DSM, hiding the complexity of the TWAIN API from the rest of the application. With EZTwain, TWAIN is still needed - all the TWAIN parts play their usual roles.  To the application, EZTWAIN presents a simplified version of the TWAIN API, plus additional features that are useful to scanning applications, such as image file loading and saving.   EZTWAIN relieves the application of the chore of routing all windows messages through TWAIN as needed, and of waiting for and reacting to the image-ready notification.