VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Синхронное получение изображений от TWAIN сканера
    В этом разделе
    VintaSoft Twain .NET SDK позволяет получать изображения от TWAIN сканера синхронно (в модальном цикле).


    Если необходимо получить изображения от TWAIN сканера синхронно, то выполните следующие действия:
    1. Откройте менеджер TWAIN устройств.
    2. Выберите TWAIN устройство.
    3. Откройте TWAIN устройство.
    4. Задайте параметры сканирования изображений.
    5. Запустите модальный цикл получения изображений от TWAIN сканера.
    6. Получайте изображения по одному пока метод Vintasoft.Twain.Device.AcquireModal возвращает значение Vintasoft.Twain.AcquireModalState.ImageAcquired.
    7. Прервите цикл когда метод Vintasoft.Twain.Device.AcquireModal вернет значение Vintasoft.Twain.AcquireModalState.None.

    Вот C#/VB.NET код, который демонстрирует, как получить изображения от TWAIN сканера в модальном цикле и сохранить полученные изображения в PDF документ:
    /// <summary>
    /// Synchronously acquire images from TWAIN device.
    /// </summary>
    public void SynchronouslyAcquireImagesFromTwainDevice()
    {
        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();
    }
    
    ''' <summary>
    ''' Synchronously acquire images from TWAIN device.
    ''' </summary>
    Public Sub SynchronouslyAcquireImagesFromTwainDevice()
            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 = System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())
                            directoryForImages = System.IO.Path.Combine(directoryForImages, "Images")
                            If Not System.IO.Directory.Exists(directoryForImages) Then
                                    System.IO.Directory.CreateDirectory(directoryForImages)
                            End If
    
                            Dim pdfFilename As String = System.IO.Path.Combine(directoryForImages, "multipage.pdf")
    
                            ' acquire image(s) from the device
                            Dim acquireModalState As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
                            Dim imageIndex As Integer = 0
                            Do
                                    acquireModalState = device.AcquireModal()
                                    Select Case 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.", System.Math.Max(System.Threading.Interlocked.Increment(imageIndex),imageIndex - 1)))
                                                    Exit Select
    
                                            Case Vintasoft.Twain.AcquireModalState.ScanCompleted
                                                    ' output current state
                                                    System.Console.WriteLine("Scan completed.")
                                                    Exit Select
    
                                            Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                                                    ' output current state
                                                    System.Console.WriteLine("Scan canceled.")
                                                    Exit Select
    
                                            Case Vintasoft.Twain.AcquireModalState.ScanFailed
                                                    ' output current state
                                                    System.Console.WriteLine(String.Format("Scan failed: {0}", device.ErrorString))
                                                    Exit Select
                                    End Select
                            Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
    
                            ' close the device
                            device.Close()
    
                            ' close the device manager
                            deviceManager.Close()
                    End Using
            Catch ex As Vintasoft.Twain.TwainException
                    System.Console.WriteLine("Error: " + ex.Message)
            End Try
    
            System.Console.ReadLine()
    End Sub