Scan images using console .NET application for Windows and Linux

Blog category: TWAIN.NET

April 8, 2024

TWAIN is a standard protocol and interface (API) that defines the interaction between a program and an image scanner. TWAIN standard is being developed by TWAIN Working Group. Version 1 of TWAIN standard defines interaction with image scanners for Windows. Version 2 of TWAIN standard defines interaction with image scanners for Windows, macOS, Linux.
TWAIN standard is the most popular standard for image scanners for Windows. Almost any image scanner has a TWAIN driver for usage in Windows.
TWAIN standard is not as popular in Linux and our research (as of early 2024) showed that only Kodak provides TWAIN drivers for using their image scanners in Linux.

SANE is an application programming interface (API) that provides standardized access to raster image scanning devices (flatbed scanners, handheld scanners, etc). SANE API is a public domain and it is open to public discussion and development. SANE API is the most popular image scanner API for Linux.
Many scanner manufacturers have created SANE drivers for their image scanners. There are also SANE drivers created by SANE community. As a result, almost any modern image scanner has SANE driver that allows you to use the device in Linux.

VintaSoft Twain .NET SDK is the professional image scanning library that allows to control TWAIN or SANE scanner and acquire images in .NET application for Windows and Linux. Acquired images can be preprocessed and saved to a file or uploaded to HTTP(S) or FTP server.

VintaSoft Twain .NET SDK provides VintaSoft TWAIN .NET API for working with TWAIN device in Windows and Linux. Also VintaSoft Twain .NET SDK provides VintaSoft SANE .NET API for working with SANE device in Linux. The detailed information about VintaSoft TWAIN .NET API and VintaSoft SANE .NET API please read in the documentation for .NET developer here: https://www.vintasoft.ru/docs/vstwain-dotnet/

Here is C# code that shows how to acquire images from TWAIN image scanner in Windows and Linux:
/// <summary>
/// Synchronously acquire images images from TWAIN scanner.
/// </summary>
public void SynchronouslyAcquireImagesFromTwainScanner()
{
    try
    {
        using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
        {
            // open the device manager
            deviceManager.Open();

            // get reference to the default device
            Vintasoft.Twain.Device device = deviceManager.DefaultDevice;

            // open the device
            device.Open();

            // set acquisition parameters
            device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
            device.ShowUI = false;
            device.DisableAfterAcquire = true;
            device.PixelType = Vintasoft.Twain.PixelType.BW;

            // create directory for PDF document
            string directoryForImages = System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory());
            directoryForImages = System.IO.Path.Combine(directoryForImages, "Images");
            if (!System.IO.Directory.Exists(directoryForImages))
                System.IO.Directory.CreateDirectory(directoryForImages);

            string pdfFilename = System.IO.Path.Combine(directoryForImages, "multipage.pdf");

            // acquire image(s) from the device
            Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
            int imageIndex = 0;
            do
            {
                acquireModalState = device.AcquireModal();
                switch (acquireModalState)
                {
                    case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                        // save image to file
                        device.AcquiredImage.Save(pdfFilename);
                        // dispose acquired image
                        device.AcquiredImage.Dispose();
                        // output current state
                        System.Console.WriteLine(string.Format("Image{0} is saved.", imageIndex++));
                        break;

                    case Vintasoft.Twain.AcquireModalState.ScanCompleted:
                        // output current state
                        System.Console.WriteLine("Scan completed.");
                        break;

                    case Vintasoft.Twain.AcquireModalState.ScanCanceled:
                        // output current state
                        System.Console.WriteLine("Scan canceled.");
                        break;

                    case Vintasoft.Twain.AcquireModalState.ScanFailed:
                        // output current state
                        System.Console.WriteLine(string.Format("Scan failed: {0}", device.ErrorString));
                        break;
                }
            }
            while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);

            // close the device
            device.Close();

            // close the device manager
            deviceManager.Close();
        }
    }
    catch (Vintasoft.Twain.TwainException ex)
    {
        System.Console.WriteLine("Error: " + ex.Message);
    }

    System.Console.ReadLine();
}


Here is C# code that shows how to acquire images from SANE image scanner in Linux:
/// <summary>
/// Synchronously acquire images images from SANE scanner.
/// </summary>
public void SynchronouslyAcquireImagesFromSaneScanner()
{
    // create SANE device manager
    using (Vintasoft.Sane.SaneLocalDeviceManager deviceManager = new Vintasoft.Sane.SaneLocalDeviceManager())
    {
        // open SANE device manager
        deviceManager.Open();

        // get count of SANE devices
        int deviceCount = deviceManager.Devices.Count;
        if (deviceCount == 0)
        {
            System.Console.WriteLine("Devices are not found.");
            return;
        }

        // select the first SANE device
        Vintasoft.Sane.SaneLocalDevice device = deviceManager.Devices[0];

        // open SANE device
        device.Open();

        int imageIndex = 0;
        Vintasoft.Sane.SaneAcquiredImage acquiredImage;
        do
        {
            try
            {
                // acquire image from SANE device
                acquiredImage = device.AcquireImageSync();
                // if image is received
                if (acquiredImage != null)
                {
                    imageIndex++;
                    string filename = string.Format("scannedImage-{0}.png", imageIndex);
                    if (System.IO.File.Exists(filename))
                        System.IO.File.Delete(filename);

                    acquiredImage.Save(filename);

                    System.Console.WriteLine(string.Format("Acquired image is saved to a file '{0}'.", filename));
                }
                // if image is not received
                else
                {
                    System.Console.WriteLine("Scan is completed.");
                    break;
                }
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(string.Format("Scan is failed: {0}", ex.Message));
                break;
            }
        }
        // while device has more images to scan
        while (device.HasMoreImagesToScan);

        // close SANE device
        device.Close();

        // close SANE device manager
        deviceManager.Close();
    }

    System.Console.ReadLine();
}