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:

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.

Comments and Discussions

Dear Jaydeep,
I am Suffering from the same issue but little Diffident.
Have you reach to some Solution for OUT of memory Exception?
Because when I am scanning 100-110 pages it works Smoothly but Some time it gets Out of Memory Exception when I restart app it Again run Smoothly
Can you please Suggest

Please help on this .. when I run the program I am able to select the scanner but when I click Acquire image it gives me exception as below

LoaderLock was detected
Message: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

Hy,
First of all thanks for the posted code in c # that is perhaps unique in the network.

my question:
I have to create an application that scans a document in 16-bit gray scale and saved in TIFF format, uncompressed 600 dpi. Of course I must not use the Epson interface.
With the Epson interface there are no problems but when I try to do the same thing programmatically, I can not set the skills in the right way and I always get an error. Using C # visual studio 2010. Is there any good Samaritan who can help me?
The steps are:
1) Select Source (not required)
2) Scan the document in A4 format such as TIFF 16-bit grayscale
3) Save to file, or manage it as an array in memory.

... Before I committed suicide. Thank you very much and please excuse the language!