VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Приостановить сканирование TWAIN (остановить устройство подачи)
    В этом разделе
    Многие TWAIN сканеры оснащены устройствами автоматической подачи документов. Некоторые из этих TWAIN сканеров позволяют остановить устройство подачи, т.е. приостановить процесс получения изображений. Метод Vintasoft.Twain.Device.StopFeeder позволяет остановить устройство подачи документов.



    Вот C#/VB.NET код, который демонстрирует, как начать асинхронное сканирование изображений, остановить устройство подачи документов и продолжить асинхронное сканирование изображений:
    static bool _isScanningFinished;
    static int _step;
    static int _imageCount;
    
    
    /// <summary>
    /// Synchronously acquires one image from TWAIN device, stops the document feeder, scans the second image.
    /// </summary>
    public static void AsynchronouslyAcquireImageFromTwainDevice_StopFeeder_ContinueImageTransfer()
    {
        _isScanningFinished = false;
        _step = 0;
        _imageCount = 0;
    
        // create the device manager
        using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
        {
            deviceManager.IsTwain2Compatible = true;
            // open the device manager
            deviceManager.Open();
    
            // get the device
            string deviceName = "KODAK Scanner: i5000";
            Vintasoft.Twain.Device device = deviceManager.Devices.Find(deviceName);
            if (device == null)
                throw new System.ApplicationException(string.Format("Device '{0}' is not found.", deviceName));
    
            // subscribe to device events
            SubscribeToDeviceEvents(device);
    
            // disable device UI
            device.ShowUI = false;
            // specify that device must be closed after scan
            device.DisableAfterAcquire = true;
    
            // open the device
            device.Open();
            // specify that 2 images must be acquired from scanner
            device.XferCount = 2;
    
            // run asynchronous image acqusition
            device.Acquire();
            // wait while feeder will be stopped
            while (!_isScanningFinished)
            {
                System.Windows.Forms.Application.DoEvents();
            }
    
            if (_imageCount != 1)
            {
                // unsubscribe from device events
                UnsubscribeFromDeviceEvents(device);
    
                throw new System.ApplicationException("Wrong acquired image count.");
            }
    
            if (device.State != Vintasoft.Twain.DeviceState.TransferReady)
            {
                // unsubscribe from device events
                UnsubscribeFromDeviceEvents(device);
    
                throw new System.ApplicationException("Feeder is NOT stopped.");
            }
            System.Console.WriteLine("Feeder is stopped.");
    
            _isScanningFinished = false;
            // run asynchronous image acqusition
            device.Acquire();
            // wait while image scan will be finished
            while (!_isScanningFinished)
            {
                System.Windows.Forms.Application.DoEvents();
            }
    
            // unsubscribe from device events
            UnsubscribeFromDeviceEvents(device);
    
            if (_imageCount != 2)
                throw new System.ApplicationException("Wrong acquired image count.");
    
            // close the device
            device.Close();
        }
    }
    
    /// <summary>
    /// Image is acquired.
    /// </summary>
    private static void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
    {
        System.Console.WriteLine("Image is acquired.");
        // increment image count
        _imageCount++;
    
        // if we need stop the feeder
        if (_step == 0)
        {
            // get reference to the device
            Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
            // stop the feeder
            device.StopFeeder();
    
            // specify that feeder is stopped
            _step = 1;
            _isScanningFinished = true;
        }
    }
    
    /// <summary>
    /// Scan is completed.
    /// </summary>
    private static void device_ScanCompleted(object sender, System.EventArgs e)
    {
        System.Console.WriteLine("Scan is completed.");
    }
    
    /// <summary>
    /// Scan is canceled.
    /// </summary>
    private static void device_ScanCanceled(object sender, System.EventArgs e)
    {
        System.Console.WriteLine("Scan is canceled.");
    }
    
    /// <summary>
    /// Scan is failed.
    /// </summary>
    private static void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
    {
        System.Console.WriteLine(string.Format("Scan is failed: {0}.", e.ErrorString));
    }
    
    /// <summary>
    /// Scan is finished.
    /// </summary>
    private static void device_ScanFinished(object sender, System.EventArgs e)
    {
        _isScanningFinished = true;
    }
    
    /// <summary>
    /// Subscribes to the device events.
    /// </summary>
    private static void SubscribeToDeviceEvents(Vintasoft.Twain.Device device)
    {
        device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
        device.ScanCompleted += new System.EventHandler(device_ScanCompleted);
        device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
        device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
        device.ScanFinished += new System.EventHandler(device_ScanFinished);
    }
    
    /// <summary>
    /// Unsubscribes from the device events.
    /// </summary>
    private static void UnsubscribeFromDeviceEvents(Vintasoft.Twain.Device device)
    {
        device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
        device.ScanCompleted -= new System.EventHandler(device_ScanCompleted);
        device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
        device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
        device.ScanFinished -= new System.EventHandler(device_ScanFinished);
    }
    
    Shared _isScanningFinished As Boolean
    Shared _step As Integer
    Shared _imageCount As Integer
    
    
    ''' <summary>
    ''' Synchronously acquires one image from TWAIN device, stops the document feeder, scans the second image.
    ''' </summary>
    Public Shared Sub AsynchronouslyAcquireImageFromTwainDevice_StopFeeder_ContinueImageTransfer()
            _isScanningFinished = False
            _step = 0
            _imageCount = 0
    
            ' create the device manager
            Using deviceManager As New Vintasoft.Twain.DeviceManager()
                    deviceManager.IsTwain2Compatible = True
                    ' open the device manager
                    deviceManager.Open()
    
                    ' get the device
                    Dim deviceName As String = "KODAK Scanner: i5000"
                    Dim device As Vintasoft.Twain.Device = deviceManager.Devices.Find(deviceName)
                    If device Is Nothing Then
                            Throw New System.ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
                    End If
    
                    ' subscribe to device events
                    SubscribeToDeviceEvents(device)
    
                    ' disable device UI
                    device.ShowUI = False
                    ' specify that device must be closed after scan
                    device.DisableAfterAcquire = True
    
                    ' open the device
                    device.Open()
                    ' specify that 2 images must be acquired from scanner
                    device.XferCount = 2
    
                    ' run asynchronous image acqusition
                    device.Acquire()
                    ' wait while feeder will be stopped
                    While Not _isScanningFinished
                            System.Windows.Forms.Application.DoEvents()
                    End While
    
                    If _imageCount <> 1 Then
                            ' unsubscribe from device events
                            UnsubscribeFromDeviceEvents(device)
    
                            Throw New System.ApplicationException("Wrong acquired image count.")
                    End If
    
                    If device.State <> Vintasoft.Twain.DeviceState.TransferReady Then
                            ' unsubscribe from device events
                            UnsubscribeFromDeviceEvents(device)
    
                            Throw New System.ApplicationException("Feeder is NOT stopped.")
                    End If
                    System.Console.WriteLine("Feeder is stopped.")
    
                    _isScanningFinished = False
                    ' run asynchronous image acqusition
                    device.Acquire()
                    ' wait while image scan will be finished
                    While Not _isScanningFinished
                            System.Windows.Forms.Application.DoEvents()
                    End While
    
                    ' unsubscribe from device events
                    UnsubscribeFromDeviceEvents(device)
    
                    If _imageCount <> 2 Then
                            Throw New System.ApplicationException("Wrong acquired image count.")
                    End If
    
                    ' close the device
                    device.Close()
            End Using
    End Sub
    
    ''' <summary>
    ''' Image is acquired.
    ''' </summary>
    Private Shared Sub device_ImageAcquired(sender As Object, e As Vintasoft.Twain.ImageAcquiredEventArgs)
            System.Console.WriteLine("Image is acquired.")
            ' increment image count
            _imageCount += 1
    
            ' if we need stop the feeder
            If _step = 0 Then
                    ' get reference to the device
                    Dim device As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
                    ' stop the feeder
                    device.StopFeeder()
    
                    ' specify that feeder is stopped
                    _step = 1
                    _isScanningFinished = True
            End If
    End Sub
    
    ''' <summary>
    ''' Scan is completed.
    ''' </summary>
    Private Shared Sub device_ScanCompleted(sender As Object, e As System.EventArgs)
            System.Console.WriteLine("Scan is completed.")
    End Sub
    
    ''' <summary>
    ''' Scan is canceled.
    ''' </summary>
    Private Shared Sub device_ScanCanceled(sender As Object, e As System.EventArgs)
            System.Console.WriteLine("Scan is canceled.")
    End Sub
    
    ''' <summary>
    ''' Scan is failed.
    ''' </summary>
    Private Shared Sub device_ScanFailed(sender As Object, e As Vintasoft.Twain.ScanFailedEventArgs)
            System.Console.WriteLine(String.Format("Scan is failed: {0}.", e.ErrorString))
    End Sub
    
    ''' <summary>
    ''' Scan is finished.
    ''' </summary>
    Private Shared Sub device_ScanFinished(sender As Object, e As System.EventArgs)
            _isScanningFinished = True
    End Sub
    
    ''' <summary>
    ''' Subscribes to the device events.
    ''' </summary>
    Private Shared Sub SubscribeToDeviceEvents(device As Vintasoft.Twain.Device)
            AddHandler device.ImageAcquired, New System.EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            AddHandler device.ScanCompleted, New System.EventHandler(AddressOf device_ScanCompleted)
            AddHandler device.ScanCanceled, New System.EventHandler(AddressOf device_ScanCanceled)
            AddHandler device.ScanFailed, New System.EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            AddHandler device.ScanFinished, New System.EventHandler(AddressOf device_ScanFinished)
    End Sub
    
    ''' <summary>
    ''' Unsubscribes from the device events.
    ''' </summary>
    Private Shared Sub UnsubscribeFromDeviceEvents(device As Vintasoft.Twain.Device)
            RemoveHandler device.ImageAcquired, New System.EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device.ScanCompleted, New System.EventHandler(AddressOf device_ScanCompleted)
            RemoveHandler device.ScanCanceled, New System.EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device.ScanFailed, New System.EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device.ScanFinished, New System.EventHandler(AddressOf device_ScanFinished)
    End Sub
    


    Вот C#/VB.NET код, который демонстрирует, как запустить синхронное сканирование изображений, остановить устройство подачи документов и продолжить синхронное сканирование изображений:
    /// <summary>
    /// Synchronously acquires one image from TWAIN device, stops the document feeder, scans the second image.
    /// </summary>
    public static void SynchronouslyAcquireImageFromTwainDevice_StopFeeder_ContinueImageTransfer()
    {
        int imageCount = 0;
    
        // create the device manager
        using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
        {
            deviceManager.IsTwain2Compatible = true;
            // open the device manager
            deviceManager.Open();
    
            // get the device
            string deviceName = "KODAK Scanner: i5000";
            Vintasoft.Twain.Device device = deviceManager.Devices.Find(deviceName);
            if (device == null)
                throw new System.ApplicationException(string.Format("Device '{0}' is not found.", deviceName));
    
            // disable device UI
            device.ShowUI = false;
            // specify that device must be closed after scan
            device.DisableAfterAcquire = true;
    
            // open the device
            device.Open();
            // specify that 2 images must be acquired from scanner
            device.XferCount = 2;
    
            // run asynchronous image acquisition
            Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
            do
            {
                acquireModalState = device.AcquireModal();
                switch (acquireModalState)
                {
                    case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                        System.Console.WriteLine("Image is acquired.");
                        if (device.AcquiredImage != null)
                            device.AcquiredImage.Dispose();
    
                        imageCount++;
    
                        // stop the feeder
                        device.StopFeeder();
                        break;
    
                    case Vintasoft.Twain.AcquireModalState.ScanCompleted:
                        System.Console.WriteLine("Scan is completed.");
                        break;
    
                    case Vintasoft.Twain.AcquireModalState.ScanCanceled:
                        System.Console.WriteLine("Scan is canceled.");
                        break;
    
                    case Vintasoft.Twain.AcquireModalState.ScanFailed:
                        System.Console.WriteLine(string.Format("Scan is failed: {0}.", device.ErrorString));
                        break;
                }
            }
            while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
    
            if (imageCount != 1)
                throw new System.ApplicationException("Wrong acquired image count.");
    
            if (device.State != Vintasoft.Twain.DeviceState.TransferReady)
                throw new System.ApplicationException("Feeder is NOT stopped.");
            System.Console.WriteLine("Feeder is stopped.");
    
            // continue asynchronous image acquisition
            do
            {
                acquireModalState = device.AcquireModal();
                switch (acquireModalState)
                {
                    case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                        System.Console.WriteLine("Image is acquired.");
    
                        if (device.AcquiredImage != null)
                            device.AcquiredImage.Dispose();
    
                        imageCount++;
                        break;
    
                    case Vintasoft.Twain.AcquireModalState.ScanCompleted:
                        System.Console.WriteLine("Scan is completed.");
                        break;
    
                    case Vintasoft.Twain.AcquireModalState.ScanCanceled:
                        System.Console.WriteLine("Scan is canceled.");
                        break;
    
                    case Vintasoft.Twain.AcquireModalState.ScanFailed:
                        System.Console.WriteLine(string.Format("Scan is failed: {0}.", device.ErrorString));
                        break;
                }
            }
            while (acquireModalState != Vintasoft.Twain.AcquireModalState.None);
    
            if (imageCount != 2)
                throw new System.ApplicationException("Wrong acquired image count.");
    
            // close the device
            device.Close();
        }
    }
    
    ''' <summary>
    ''' Synchronously acquires one image from TWAIN device, stops the document feeder, scans the second image.
    ''' </summary>
    Public Shared Sub SynchronouslyAcquireImageFromTwainDevice_StopFeeder_ContinueImageTransfer()
            Dim imageCount As Integer = 0
    
            ' create the device manager
            Using deviceManager As New Vintasoft.Twain.DeviceManager()
                    deviceManager.IsTwain2Compatible = True
                    ' open the device manager
                    deviceManager.Open()
    
                    ' get the device
                    Dim deviceName As String = "KODAK Scanner: i5000"
                    Dim device As Vintasoft.Twain.Device = deviceManager.Devices.Find(deviceName)
                    If device Is Nothing Then
                            Throw New System.ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
                    End If
    
                    ' disable device UI
                    device.ShowUI = False
                    ' specify that device must be closed after scan
                    device.DisableAfterAcquire = True
    
                    ' open the device
                    device.Open()
                    ' specify that 2 images must be acquired from scanner
                    device.XferCount = 2
    
                    ' run asynchronous image acquisition
                    Dim acquireModalState As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
                    Do
                            acquireModalState = device.AcquireModal()
                            Select Case acquireModalState
                                    Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                                            System.Console.WriteLine("Image is acquired.")
                                            If device.AcquiredImage IsNot Nothing Then
                                                    device.AcquiredImage.Dispose()
                                            End If
    
                                            imageCount += 1
    
                                            ' stop the feeder
                                            device.StopFeeder()
                                            Exit Select
    
                                    Case Vintasoft.Twain.AcquireModalState.ScanCompleted
                                            System.Console.WriteLine("Scan is completed.")
                                            Exit Select
    
                                    Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                                            System.Console.WriteLine("Scan is canceled.")
                                            Exit Select
    
                                    Case Vintasoft.Twain.AcquireModalState.ScanFailed
                                            System.Console.WriteLine(String.Format("Scan is failed: {0}.", device.ErrorString))
                                            Exit Select
                            End Select
                    Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
    
                    If imageCount <> 1 Then
                            Throw New System.ApplicationException("Wrong acquired image count.")
                    End If
    
                    If device.State <> Vintasoft.Twain.DeviceState.TransferReady Then
                            Throw New System.ApplicationException("Feeder is NOT stopped.")
                    End If
                    System.Console.WriteLine("Feeder is stopped.")
    
                    ' continue asynchronous image acquisition
                    Do
                            acquireModalState = device.AcquireModal()
                            Select Case acquireModalState
                                    Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                                            System.Console.WriteLine("Image is acquired.")
    
                                            If device.AcquiredImage IsNot Nothing Then
                                                    device.AcquiredImage.Dispose()
                                            End If
    
                                            imageCount += 1
                                            Exit Select
    
                                    Case Vintasoft.Twain.AcquireModalState.ScanCompleted
                                            System.Console.WriteLine("Scan is completed.")
                                            Exit Select
    
                                    Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                                            System.Console.WriteLine("Scan is canceled.")
                                            Exit Select
    
                                    Case Vintasoft.Twain.AcquireModalState.ScanFailed
                                            System.Console.WriteLine(String.Format("Scan is failed: {0}.", device.ErrorString))
                                            Exit Select
                            End Select
                    Loop While acquireModalState <> Vintasoft.Twain.AcquireModalState.None
    
                    If imageCount <> 2 Then
                            Throw New System.ApplicationException("Wrong acquired image count.")
                    End If
    
                    ' close the device
                    device.Close()
            End Using
    End Sub