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.

hello,I have a ADF question.Can you help me
The first come to here. I use the NET TWAIN image scanner.
But I Selet ADF Option, [Document In ADF] set more than 1 value，
then Scan button click.The TWAIN DS window display all long,
When Scan button click,I want to close the TWAIN window.
What Can I Do?Help Me..

hello,I have an ADF question.Can you help me?
The first come to here. I use the NET TWAIN image scanner.
But I Selet ADF Option, [Document In ADF] set more than 1 value，
then Scan button click.The TWAIN DS window display all long,
When Scan button click,I want to close the TWAIN window.
What Can I Do?Help Me..

Dear All,
I am new to this 'TWAIN' application, I just downloaded this sample application and its worked fine for me.
I want to use my own UI for this application, so I modified guif.ShowUI = 0; in Acquire() function. Its working.
1. I want to use ADF and Flatbed, I checked this post and found one solution that was
TwCapability cap = new TwCapability(TwCap.XferCount, -1);// changed 1 to -1. but its not working.
But in twain 2.1 specification they says to set CAP_AUTOFEED,CAP_FEEDERENABLED etc, but i could not found these fields in TwainDefs.cs
2. when I select color/Gray scale/Black & White from my UI , how to handle this scanning according to my selection.

How to make these 2 things in my application? Should I need to add anything in TwainDefs.cs and TwainLib.cs?
Please help....thanks ..

I downloaded the demo and the source code. The demo works as advertised. However trying to run through the ID in 2010 allows for the selection of the scanning device but does not launch the scanning tool. I tried in both the debug and release version. If I double click the release or debug exe in the bin file it runs correctly.

Hi, Does anyone know how to return a List of paper sizes the scanner supports? That is the only capability I want to allow the user to modify. Therefore, I do not want to open the scanners setup utility User Interface. Thank you.

It's up to the scanner, and most of them won't tell you. The TWAIN spec actually says: "Allowed values: Any value"
You can try values and see which ones the driver rejects with TWCC_BADVALUE...
Why do you need to know? Gamma values below 1.0 are pretty rare & weird, so are gamma values above 3.0. If you just need to configure a slider or a spin control, that's not a bad range.

I am using the VB version of this code and have modified the code to save the documents
instead of showing them. But the pages save in individual files.
What code do I need to save all pages in 1 file?
The code is as follows:

I'm doing this using PDFSharp (http://www.pdfsharp.net/?AspxAutoDetectCookieSupport=1), which is a great library for working with PDFs. I'm saving each page to a file, and using PDFSharp to write a PDF with all images. Here's a code snippit:

I'm doing a project relative to TWAIN Spec in C#
I have a class TwCapability like struct TW_CAPABILITY
It's OK when I set value (-1000 -> 1000) for ICAP_BRIGHTNESS or ICAP_CONTRAST
But I want to set value for ICAP_GAMMA with float value (for ex. 0.2) like TWAIN's toolkit Twack_32 does!

Btw: Return code when I set value 0.2 for ICAP_GAMMA is TWRC_SUCCESS, but nothing happend with my output image!

hi.
thanks for your good article;
I use this source code and I have a problem:
I want to change saving progress: first save the picture in a temp directory, then send it to a ftp server.
but first of all, I need to know how get or set the path of save dialog window when calling GdiPlus.SaveDIBAs
help me please