Click here to Skip to main content
14,388,831 members

.NET TWAIN image scanner

Rate this:
4.91 (224 votes)
Please Sign up or sign in to vote.
4.91 (224 votes)
12 May 2002Public Domain
Using TWAIN API to scan images

Sample Screenshot


In Windows imaging applications, the most used API for scanning is TWAIN 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.


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:

[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:

class Twain

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...)


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).


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


About the Author

Web Developer
Switzerland Switzerland
No Biography provided

Comments and Discussions

GeneralDuplex Mode Pin
morke4-Jan-10 2:49
Membermorke4-Jan-10 2:49 
Generaltwain_32.dll question Pin
DroneBG21-Dec-09 4:02
MemberDroneBG21-Dec-09 4:02 
GeneralRe: twain_32.dll question Pin
NitinMakwana9-Feb-10 1:21
MemberNitinMakwana9-Feb-10 1:21 
Generalthere's nothing un-modern or '16-bit' about TWAIN Pin
Spike0xFF18-Dec-09 15:33
MemberSpike0xFF18-Dec-09 15:33 
Questionhow to save TWAIN Scanning image into pdf file Pin
Member 421353221-Nov-09 13:30
MemberMember 421353221-Nov-09 13:30 
AnswerRe: how to save TWAIN Scanning image into pdf file Pin
AmitSood198521-Jul-10 0:58
MemberAmitSood198521-Jul-10 0:58 
GeneralHello NetMaster Pin
Mike Kostersitz11-Nov-09 2:27
MemberMike Kostersitz11-Nov-09 2:27 
Generali need the code with vb Pin
alaa9216112-Nov-09 0:54
Memberalaa9216112-Nov-09 0:54 
QuestionHelp on Blank Page Detection Pin
geetesh.12331-Oct-09 3:30
Membergeetesh.12331-Oct-09 3:30 
Questionnewbie question: mdi child form problem. Pin
dude25175418-Oct-09 19:49
Memberdude25175418-Oct-09 19:49 
QuestionConnect to network scanner Pin
ihsan4u28-Sep-09 1:17
Memberihsan4u28-Sep-09 1:17 
AnswerRe: Connect to network scanner Pin
James West1-Nov-09 13:08
MemberJames West1-Nov-09 13:08 
AnswerEpson Supported Twain Commands Pin
JohnSims19421-Sep-09 2:07
MemberJohnSims19421-Sep-09 2:07 
AnswerScanner with Document Feeder PinPopular
JohnSims19421-Sep-09 2:05
MemberJohnSims19421-Sep-09 2:05 
GeneralRe: Scanner with Document Feeder Pin
Member 39132835-Nov-09 0:23
MemberMember 39132835-Nov-09 0:23 
GeneralRe: Scanner with Document Feeder Pin
Member 39132835-Nov-09 0:35
MemberMember 39132835-Nov-09 0:35 
AnswerRe: Scanner with Document Feeder Pin
JohnSims1945-Nov-09 0:50
MemberJohnSims1945-Nov-09 0:50 
GeneralRe: Scanner with Document Feeder Pin
zlov29-Sep-10 1:17
Memberzlov29-Sep-10 1:17 
QuestionPassing a string to TwCapability Pin
JohnSims19421-Sep-09 1:46
MemberJohnSims19421-Sep-09 1:46 
GeneralDSuserif will cause exception AccessViolationException: Pin
carlshe19-Sep-09 21:17
Membercarlshe19-Sep-09 21:17 
GeneralRe: DSuserif will cause exception AccessViolationException: Pin
LupinTheThird30-Oct-11 11:50
MemberLupinTheThird30-Oct-11 11:50 
QuestionHow to implement it to the User Control Pin
chandresh.softobiz16-Sep-09 18:50
Memberchandresh.softobiz16-Sep-09 18:50 
GeneralIs it possible to scan just one document from feeder Pin
zodiacseven11-Aug-09 4:20
Memberzodiacseven11-Aug-09 4:20 
GeneralUse Duplex with Driver Twain - Help URGENT Pin
IvesBertoli30-Jul-09 3:44
MemberIvesBertoli30-Jul-09 3:44 
GeneralRe: Use Duplex with Driver Twain - Help URGENT Pin
JohnSims19418-Sep-09 1:47
MemberJohnSims19418-Sep-09 1:47 

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.

Posted 16 Sep 2001


418 bookmarked