Click here to Skip to main content
15,860,859 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   132K   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

 
GeneralRe: Get Capabilities [modified] Pin
Chris Shaw14-Feb-11 3:29
Chris Shaw14-Feb-11 3:29 
GeneralRe: Get Capabilities [modified] Pin
Christopher Walsalam15-Dec-13 23:57
Christopher Walsalam15-Dec-13 23:57 
GeneralDetect scanner problem Pin
jymitra9-Feb-11 0:41
jymitra9-Feb-11 0:41 
GeneralUse Driver default interface Pin
Member 473569115-Jan-11 2:00
Member 473569115-Jan-11 2:00 
GeneralNetwork access for Twain Pin
satiss7pwr26-Dec-10 19:50
satiss7pwr26-Dec-10 19:50 
Generalcode control scanning Pin
huihui122926-Dec-10 16:03
huihui122926-Dec-10 16:03 
GeneralAcquire sos Pin
huihui122922-Dec-10 16:18
huihui122922-Dec-10 16:18 
GeneralRe: Acquire sos Pin
drazenmd24-Dec-10 1:51
drazenmd24-Dec-10 1:51 
GeneralTrouble with twaindotnet on Windows 7 (64 bit) Pin
J. Bauer15-Dec-10 5:13
J. Bauer15-Dec-10 5:13 
GeneralRe: Trouble with twaindotnet on Windows 7 (64 bit) PinPopular
Daniela_6616-Dec-10 1:20
Daniela_6616-Dec-10 1:20 
GeneralRe: Trouble with twaindotnet on Windows 7 (64 bit) Pin
liffo8229-May-11 23:32
liffo8229-May-11 23:32 
GeneralWrite image to file without opening for view Pin
J Queen7-Dec-10 4:28
J Queen7-Dec-10 4:28 
QuestionException Device Not Found Pin
drazenmd7-Dec-10 1:05
drazenmd7-Dec-10 1:05 
GeneralFigure out all of the settings Pin
surfer2615-Dec-10 10:44
surfer2615-Dec-10 10:44 
GeneralRe: Figure out all of the settings Pin
hehengw19827-Jan-11 5:56
hehengw19827-Jan-11 5:56 
GeneralCreating a simple method that returns an System.Drawing.Image. Pin
stapia.gutierrez2-Dec-10 3:34
stapia.gutierrez2-Dec-10 3:34 
Generalsettings inside code ( Page Size, dpi, scanning side, deskew, scanning option) Pin
surfer26130-Nov-10 10:05
surfer26130-Nov-10 10:05 
GeneralRe: settings inside code ( Page Size, dpi, scanning side, deskew, scanning option) Pin
Raghava jogu16-Dec-10 20:37
Raghava jogu16-Dec-10 20:37 
General64bit twain Pin
mafish30-Nov-10 1:34
mafish30-Nov-10 1:34 
QuestionWin 7 64bit problem? Pin
Edgar R. C.13-Nov-10 17:24
Edgar R. C.13-Nov-10 17:24 
AnswerRe: Win 7 64bit problem? Pin
m_cozza27-Apr-11 5:43
m_cozza27-Apr-11 5:43 
QuestionI want to show UI only,but not transfer the image to program. Pin
zouy9-Nov-10 2:33
zouy9-Nov-10 2:33 
Generalconvert from intpr to bitmap Pin
Tamer Hatoum4-Nov-10 12:56
Tamer Hatoum4-Nov-10 12:56 
QuestionDuplex capabilty get "Failure" Pin
jymitra26-Oct-10 4:18
jymitra26-Oct-10 4:18 
Questionshow UI hang application(windows 7 64bit ) Pin
jymitra18-Oct-10 19:53
jymitra18-Oct-10 19:53 

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.