How to access TWAIN scanners in C# .Net?

In the previous post, we have talked about how to call the TWAIN interface in .NET. Generally you can either code from scratch or use a third-party .NET wrapper class for TWAIN. In this post, we will focus on how to scan documents from TWAIN scanners in C# by using Dynamic .NET TWAIN, a TWAIN scanning & webcam .NET component.

With Dynamic .NET TWAIN SDK, you can deploy a .NET document imaging application practically in a couple of days. You can use the SDK to access TWAIN scanners and also DirectShow compatible webcams. When capturing the images, you can customize the scanning settings like ADF, duplex, resolution, pixel type, page size and more. After the documents are scanned, you can do editing – crop, rotate, erase etc. and save the images to your local disk, FTP site, web server or document repository.

Access TWAIN Scanners in C#

Below is the C# code snippet of acquiring images from TWAIN scanners using Dynamic .NET TWAIN SDK.

using Dynamsoft.DotNet.TWAIN;

private void AcquireImage()
        {
            try
            {
                dynamicDotNetTwain.SelectSourceByIndex((short)cbxSource.SelectedIndex); // Select the TWAIN device
                dynamicDotNetTwain.OpenSource();

                dynamicDotNetTwain.IfShowUI = chkShowUI.Checked; //whether to show the TWAIN user interface
                dynamicDotNetTwain.IfFeederEnabled = chkADF.Checked;//scan from auto feeder or flatbed
                dynamicDotNetTwain.IfAutoFeed = chkADF.Checked;
                dynamicDotNetTwain.IfDuplexEnabled = chkDuplex.Checked; //whether to enable duplex scanning
                dynamicDotNetTwain.IfDisableSourceAfterAcquire = true;

                //set the pixel for scanning
                if (rdbtnBW.Checked)
                {
                    dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_BW;
                    dynamicDotNetTwain.BitDepth = 1;
                }
                else if (rdbtnGray.Checked)
                {
                    dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_GRAY;
                    dynamicDotNetTwain.BitDepth = 8;
                }
                else
                {
                    dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_RGB;
                    dynamicDotNetTwain.BitDepth = 24;
                }

                dynamicDotNetTwain.Resolution = int.Parse(cbxResolution.Text); //set the resolution of the images

                dynamicDotNetTwain.AcquireImage();// Acquire image from the TWAIN device
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("An exception occurs: " + ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

Download & Try Demo Application

To see Dynamic .NET TWAIN in action, you can download and try the demo applications from this page.

If you want to use Dynamic .NET TWAIN SDK to in your own document imaging/management application , get 30-day free trial here.
Download Free Trial

If you are need a TWAIN ActiveX/Plugin for your ASP.NET (C#/VB.NET) web application, Dynamic Web TWAIN would be what you need.

Let us know in the comments section below if you have any questions on scanning documents with C#.

Subscribe Newsletter

Subscribe to our mailing list to get the monthly update.

Subscribename@email.com