VintaSoft Twain .NET SDK 12.1
In This Topic
    Acquire images from TWAIN scanner synchronously
    In This Topic
    VintaSoft Twain .NET SDK allows to acquire images from TWAIN 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.

    Here is an example that demonstrates how to acquire images in modal loop using TWAIN scanner driver user interface and save acquired images into PDF document:
    namespace TwainExamples_CSharp
    {
        class SyncScan
        {
    
            /// <summary>
            /// Acquire images synchronously.
            /// </summary>
            public void AcquireImagesSynchronously()
            {
                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();
            }
    
        }
    }
    
    
    Class SyncScan
    
        ''' <summary>
        ''' Acquire images synchronously.
        ''' </summary>
        Public Sub AcquireImagesSynchronously()
            Try
                Using deviceManager As New Vintasoft.Twain.DeviceManager()
                    ' open the device manager
                    deviceManager.Open()
    
                    ' get reference to the default device
                    Dim device As Vintasoft.Twain.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
                    Dim directoryForImages As String = IO.Path.GetDirectoryName(IO.Directory.GetCurrentDirectory())
                    directoryForImages = IO.Path.Combine(directoryForImages, "Images")
                    If Not IO.Directory.Exists(directoryForImages) Then
                        IO.Directory.CreateDirectory(directoryForImages)
                    End If
    
                    Dim pdfFilename As String = IO.Path.Combine(directoryForImages, "multipage.pdf")
    
                    ' acquire image(s) from the device
                    Dim acquireModalState1 As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
                    Dim imageIndex As Integer = 0
                    Do
                        acquireModalState1 = device.AcquireModal()
                        Select Case acquireModalState1
                            Case Vintasoft.Twain.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 Vintasoft.Twain.AcquireModalState.ScanCompleted
                                ' output current state
                                Console.WriteLine("Scan completed.")
                                Exit Select
    
                            Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                                ' output current state
                                Console.WriteLine("Scan canceled.")
                                Exit Select
    
                            Case Vintasoft.Twain.AcquireModalState.ScanFailed
                                ' output current state
                                Console.WriteLine(String.Format("Scan failed: {0}", device.ErrorString))
                                Exit Select
                        End Select
                    Loop While acquireModalState1 <> Vintasoft.Twain.AcquireModalState.None
    
                    ' close the device
                    device.Close()
    
                    ' close the device manager
                    deviceManager.Close()
                End Using
            Catch ex As Vintasoft.Twain.TwainException
                Console.WriteLine("Error: " + ex.Message)
            End Try
    
            Console.ReadLine()
        End Sub
    
    End Class