Click here to Skip to main content
14,393,257 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

AnswerRe: How to scan and show on picturebox vc# 2008 Pin
marcoscarrera21-Jun-11 20:46
Membermarcoscarrera21-Jun-11 20:46 
QuestionHow can i return to multiple images when i using TWAIN API to scan images ? Pin
visatest11-Apr-10 16:23
Membervisatest11-Apr-10 16:23 
AnswerRe: How can i return to multiple images when i using TWAIN API to scan images ? Pin
Wolexie20-May-10 7:12
MemberWolexie20-May-10 7:12 
GeneralTransfer Buffered Memory Mode ISSUE Pin
matheo7-Apr-10 4:19
Membermatheo7-Apr-10 4:19 
GeneralRe: Transfer Buffered Memory Mode ISSUE Pin
fabioRGV4-Mar-11 1:22
MemberfabioRGV4-Mar-11 1:22 
QuestionAbility to take a snapshot from a tv card video feed Pin
to_aru_shiroi_neko5-Apr-10 22:31
Memberto_aru_shiroi_neko5-Apr-10 22:31 
AnswerRe: Ability to take a snapshot from a tv card video feed Pin
sirk123au31-Jul-10 16:58
Membersirk123au31-Jul-10 16:58 
GeneralUse Twain DLL 64 bit Pin
Alessandro Test3-Mar-10 4:52
MemberAlessandro Test3-Mar-10 4:52 
QuestionI converted this code for and got errors Pin
rheder28-Feb-10 16:24
Memberrheder28-Feb-10 16:24 
Hi All,

I have this code in 05 and its working very well,however at the following line I got type mismatche..any knows how to fix that?

bmi.biSizeImage = Int((((bmi.biWidth * bmi.biBitCount) + 31) & Hex(Not (31))) / 2 ^ 3) * bmi.biHeight

error message after scanning - cannot convert string {"HEXNUMBER"} to double.

help me please.

AnswerRe: I converted this code for and got errors Pin
Alessandro Test3-Mar-10 4:45
MemberAlessandro Test3-Mar-10 4:45 
QuestionHow to used scanner in vs2008 aspx Pin
Member 426204224-Feb-10 0:56
MemberMember 426204224-Feb-10 0:56 
GeneralComponentize Pin
Member 383947323-Feb-10 23:15
MemberMember 383947323-Feb-10 23:15 
GeneralSave Image To folder Without Opening Save dialogbox Pin
NitinMakwana14-Feb-10 20:18
MemberNitinMakwana14-Feb-10 20:18 
GeneralRe: Save Image To folder Without Opening Save dialogbox Pin
NitinMakwana17-Feb-10 3:40
MemberNitinMakwana17-Feb-10 3:40 
GeneralRe: Save Image To folder Without Opening Save dialogbox Pin
Alessandro Test3-Mar-10 4:50
MemberAlessandro Test3-Mar-10 4:50 
GeneralRe: Save Image To folder Without Opening Save dialogbox Pin
NitinMakwana4-Mar-10 21:30
MemberNitinMakwana4-Mar-10 21:30 
GeneralRe: Save Image To folder Without Opening Save dialogbox Pin
Abdullah Çetinkaya23-Jan-13 2:15
MemberAbdullah Çetinkaya23-Jan-13 2:15 
GeneralExtended Image Info. Pin
MonkFox8-Feb-10 12:18
MemberMonkFox8-Feb-10 12:18 
GeneralRe: Extended Image Info. Pin
MonkFox8-Feb-10 12:28
MemberMonkFox8-Feb-10 12:28 
GeneralProblem of Setting Brightness Pin
Member 260553419-Jan-10 1:49
MemberMember 260553419-Jan-10 1:49 
GeneralRe: Problem of Setting Brightness Pin
Member 260553420-Jan-10 19:38
MemberMember 260553420-Jan-10 19:38 
GeneralRe: Problem of Setting Brightness Pin
Member 260553420-Jan-10 20:41
MemberMember 260553420-Jan-10 20:41 
AnswerRe: Problem of Setting Brightness Pin
Spike0xFF28-Jan-10 15:56
MemberSpike0xFF28-Jan-10 15:56 
GeneralCapability Brightness/Constrast Could not Work on the image Pin
Member 260553418-Jan-10 2:36
MemberMember 260553418-Jan-10 2:36 
QuestionAcquire images in picture box from scanner [modified] Pin
manzoor4u18-Jan-10 1:28
Membermanzoor4u18-Jan-10 1:28 

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