VintaSoft Twain .NET SDK v10.2
Acquire images from scanner synchronously

VintaSoft Twain .NET SDK allows to acquire images from a scanner synchronously (in a modal loop).

Here is to do list for acquiring images synchronously:

  1. Open TWAIN device manager.
  2. Select TWAIN device.
  3. Open TWAIN device.
  4. Set the scanning settings.
  5. Start the acquisition modal loop.
  6. Retrieve acquired images one by one while the Device.AcquireModal method returns AcquireModalState.ImageAcquired.
  7. Break loop if the Device.AcquireModal method returns AcquireModalState.None.

Example: Here is an example that shows how to acquire images in modal loop using scanner driver user interface and save acquired images into PDF document:
Imports System.IO
Imports Vintasoft.Twain

Class SyncScan

    ''' <summary>
    ''' Acquire images synchronously.
    ''' </summary>
    Public Sub AcquireImagesSynchronously()
        Try
            Using deviceManager As New DeviceManager()
                ' open the device manager
                deviceManager.Open()

                ' get reference to the default device
                Dim device As Device = deviceManager.DefaultDevice

                ' open the device
                device.Open()

                ' set acquisition parameters
                device.TransferMode = TransferMode.Memory
                device.ShowUI = False
                device.DisableAfterAcquire = True
                device.PixelType = PixelType.BW

                ' create directory for PDF document
                Dim directoryForImages As String = Path.GetDirectoryName(Directory.GetCurrentDirectory())
                directoryForImages = Path.Combine(directoryForImages, "Images")
                If Not Directory.Exists(directoryForImages) Then
                    Directory.CreateDirectory(directoryForImages)
                End If

                Dim pdfFilename As String = Path.Combine(directoryForImages, "multipage.pdf")

                ' acquire image(s) from the device
                Dim acquireModalState1 As AcquireModalState = AcquireModalState.None
                Dim imageIndex As Integer = 0
                Do
                    acquireModalState1 = device.AcquireModal()
                    Select Case acquireModalState1
                        Case AcquireModalState.ImageAcquired
                            ' save image to file
                            device.AcquiredImage.Save(pdfFilename)
                            ' dispose acquired image
                            device.AcquiredImage.Dispose()
                            ' output current state
                            Console.WriteLine(String.Format("Image{0} is saved.", System.Math.Max(System.Threading.Interlocked.Increment(imageIndex), imageIndex - 1)))
                            Exit Select

                        Case AcquireModalState.ScanCompleted
                            ' output current state
                            Console.WriteLine("Scan completed.")
                            Exit Select

                        Case AcquireModalState.ScanCanceled
                            ' output current state
                            Console.WriteLine("Scan canceled.")
                            Exit Select

                        Case AcquireModalState.ScanFailed
                            ' output current state
                            Console.WriteLine(String.Format("Scan failed: {0}", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState1 <> AcquireModalState.None

                ' close the device
                device.Close()

                ' close the device manager
                deviceManager.Close()
            End Using
        Catch ex As TwainException
            Console.WriteLine("Error: " + ex.Message)
        End Try

        Console.ReadLine()
    End Sub

End Class
              
using System;
using System.IO;
using Vintasoft.Twain;

namespace TwainExamples_CSharp
{
    class SyncScan
    {

        /// <summary>
        /// Acquire images synchronously.
        /// </summary>
        public void AcquireImagesSynchronously()
        {
            try
            {
                using (DeviceManager deviceManager = new DeviceManager())
                {
                    // open the device manager
                    deviceManager.Open();

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

                    // open the device
                    device.Open();

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

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

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

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

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

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

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

                    // close the device
                    device.Close();

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

            Console.ReadLine();
        }

    }
}