Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

.NET TWAIN image scanner

Rate me:
Please Sign up or sign in to vote.
4.91/5 (227 votes)
12 May 2002Public Domain2 min read 7.1M   132.1K   421   996
Using TWAIN API to scan images

Sample Screenshot

Abstract

In Windows imaging applications, the most used API for scanning is TWAIN www.twain.org. Unfortunately, the new .NET Framework has no built-in support for TWAIN. So we have to work with the interop methods of .NET to access this API. This article doesn't explain this interop techniques, and good knowledge of the TWAIN 1.9 specifications is assumed! The sample code included doesn't present a finished library, only some essential steps for a minimal TWAIN adaption to .NET applications.

Details

First step was to port the most important parts of TWAIN.H, these are found in TwainDefs.cs. The real logic for calling TWAIN is coded in the class Twain, in file TwainLib.cs.. As the TWAIN API is exposed by the Windows DLL, twain_32.dll, we have to use the .NET DllImport mechanism for interop with legacy code. This DLL has the central DSM_Entry(), ordinal #1 function exported as the entry point to TWAIN. This call has numerous parameters, and the last one is of variable type! It was found to be best if we declare multiple variants of the call like:

C#
[DllImport("twain_32.dll", EntryPoint="#1")]
private static extern TwRC DSMparent(
    [In, Out] TwIdentity origin,
    IntPtr zeroptr,
    TwDG dg, TwDAT dat, TwMSG msg,
    ref IntPtr refptr );

The Twain class has a simple 5-step interface:

C#
class Twain
{
    Init();
    Select();
    Acquire();
    PassMessage();
    TransferPictures();
}

For some sort of 'callbacks', TWAIN uses special Windows messages, and these must be caught from the application-message-loop. In .NET, the only way found was IMessageFilter.PreFilterMessage(), and this filter has to be activated with a call like Application.AddMessageFilter(). Within the filter method, we have to forward each message to Twain.PassMessage(), and we get a hint (enum TwainCommand) back for how we have to react.

Sample App

The sample is a Windows Forms MDI-style application. It has the two TWAIN-related menu items Select Source... and Acquire... Once an image is scanned in, we can save it to a file in any of the GDI+ supported file formats (BMP, GIF, TIFF, JPEG...)

Limitations

All code was only tested on Windows 2000SP2, with an Epson Perfection USB scanner and an Olympus digital photo camera. The scanned picture is (by TWAIN spec) a Windows DIB, and the sample code has VERY little checking against error return codes and bitmap formats. Unfortunately, no direct method is available in .NET to convert a DIB to the managed Bitmap class... Some known problems may show up with color palettes and menus.

Note, TWAIN has it's root in 16-Bit Windows! For a more modern API supported on Windows ME/XP, have a look at Windows Image Acquisition (WIA).

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCapture image frm webcam Pin
shahzad arfan10-Apr-04 6:16
sussshahzad arfan10-Apr-04 6:16 
GeneralRe: Capture image frm webcam Pin
Mr.Naveed10-Apr-04 8:51
Mr.Naveed10-Apr-04 8:51 
GeneralRe: Capture image frm webcam Pin
Anonymous14-Apr-04 3:38
Anonymous14-Apr-04 3:38 
GeneralRe: Capture image frm webcam Pin
Member 373470329-Dec-09 22:06
Member 373470329-Dec-09 22:06 
GeneralJPEG Quality Pin
mcfisto19-Mar-04 0:14
mcfisto19-Mar-04 0:14 
GeneralSetting Resolution Pin
bill davidheiser1-Mar-04 11:37
bill davidheiser1-Mar-04 11:37 
GeneralRe: Setting values Pin
ElGuroProgramador23-Mar-04 8:18
ElGuroProgramador23-Mar-04 8:18 
GeneralGet TwCap Properties Pin
ElGuroProgramador23-Feb-04 20:05
ElGuroProgramador23-Feb-04 20:05 
GeneralRe: Get TwCap Properties Pin
Steve Clarke7-May-04 10:49
Steve Clarke7-May-04 10:49 
GeneralRe: Get TwCap Properties Pin
Ming_Z10-Jul-06 10:34
Ming_Z10-Jul-06 10:34 
GeneralTO VB .NET Pin
Anonymous23-Feb-04 0:36
Anonymous23-Feb-04 0:36 
GeneralRe: TO VB .NET Pin
abohmann8-May-04 23:41
abohmann8-May-04 23:41 
GeneralUse of component with hp scanners Pin
alexAreaIT15-Feb-04 22:29
alexAreaIT15-Feb-04 22:29 
GeneralScan paper of manual source Pin
Ignacio Lopez3-Feb-04 0:51
Ignacio Lopez3-Feb-04 0:51 
GeneralWebCam Go generates stackoverflow Pin
Alexsander Antunes28-Jan-04 13:13
professionalAlexsander Antunes28-Jan-04 13:13 
GeneralGetting the bitmap to a temporary file Pin
27-Jan-04 2:41
suss27-Jan-04 2:41 
GeneralRe: Getting the bitmap to a temporary file Pin
bill314152-Mar-04 4:48
bill314152-Mar-04 4:48 
GeneralRe: Getting the bitmap to a temporary file Pin
80x8621-May-04 1:33
80x8621-May-04 1:33 
GeneralRe: Getting the bitmap to a temporary file Pin
dcoolidge17-Mar-04 14:27
dcoolidge17-Mar-04 14:27 
GeneralRe: Getting the bitmap to a temporary file Pin
80x8621-May-04 1:32
80x8621-May-04 1:32 
GeneralRe: Getting the bitmap to a temporary file Pin
80x8621-May-04 1:33
80x8621-May-04 1:33 
GeneralPaper ready Pin
robstove19-Jan-04 19:04
robstove19-Jan-04 19:04 
Generalactivatign a scanner through a button.... Pin
gin_blossoms18-Jan-04 7:18
gin_blossoms18-Jan-04 7:18 
GeneralRe: activatign a scanner through a button.... Pin
fifthwheel11-Feb-04 14:26
fifthwheel11-Feb-04 14:26 
GeneralRe: activatign a scanner through a button.... Pin
mlkeS22-Feb-04 20:08
mlkeS22-Feb-04 20:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.