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


    Вот пример, который демонстрирует, как начать асинхронное получение изображения, остановить устройство подачи документов и продолжить асинхронное получение изображения:
    namespace TwainExamples_CSharp
    {
        class AsyncScan_StopFeeder
        {
    
            static bool _isScanFinished;
            static int _step;
            static int _imageCount;
    
    
    
            /// <summary>
            /// Scans the first image, stops the document feeder, scans the second image.
            /// </summary>
            public static void ScanImage_StopFeeder_ScanImage()
            {
                _isScanFinished = 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 (!_isScanFinished)
                    {
                        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.");
    
                    _isScanFinished = false;
                    // run asynchronous image acqusition
                    device.Acquire();
                    // wait while image scan will be finished
                    while (!_isScanFinished)
                    {
                        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;
                    _isScanFinished = 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)
            {
                _isScanFinished = 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);
            }
    
        }
    }
    
    
    Class AsyncScan_StopFeeder
    
        Shared _isScanFinished As Boolean
        Shared _step As Integer
        Shared _imageCount As Integer
    
    
    
        ''' <summary>
        ''' Scans the first image, stops the document feeder, scans the second image.
        ''' </summary>
        Public Shared Sub ScanImage_StopFeeder_ScanImage()
            _isScanFinished = 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 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 _isScanFinished
                    System.Windows.Forms.Application.DoEvents()
                End While
    
                If _imageCount <> 1 Then
                    ' unsubscribe from device events
                    UnsubscribeFromDeviceEvents(device)
    
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                If device.State <> Vintasoft.Twain.DeviceState.TransferReady Then
                    ' unsubscribe from device events
                    UnsubscribeFromDeviceEvents(device)
    
                    Throw New ApplicationException("Feeder is NOT stopped.")
                End If
                Console.WriteLine("Feeder is stopped.")
    
                _isScanFinished = False
                ' continue asynchronous image acqusition
                device.Acquire()
                ' wait while image scan will be finished
                While Not _isScanFinished
                    System.Windows.Forms.Application.DoEvents()
                End While
    
                ' unsubscribe from device events
                UnsubscribeFromDeviceEvents(device)
    
                If _imageCount <> 2 Then
                    Throw New 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(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
            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
                _isScanFinished = True
            End If
        End Sub
    
        ''' <summary>
        ''' Scan is completed.
        ''' </summary>
        Private Shared Sub device_ScanCompleted(ByVal sender As Object, ByVal e As EventArgs)
            Console.WriteLine("Scan is completed.")
        End Sub
    
        ''' <summary>
        ''' Scan is canceled.
        ''' </summary>
        Private Shared Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
            Console.WriteLine("Scan is canceled.")
        End Sub
    
        ''' <summary>
        ''' Scan is failed.
        ''' </summary>
        Private Shared Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
            Console.WriteLine(String.Format("Scan is failed: {0}.", e.ErrorString))
        End Sub
    
        ''' <summary>
        ''' Scan is finished.
        ''' </summary>
        Private Shared Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            _isScanFinished = True
        End Sub
    
        ''' <summary>
        ''' Subscribes to the device events.
        ''' </summary>
        Private Shared Sub SubscribeToDeviceEvents(ByVal device As Vintasoft.Twain.Device)
            AddHandler device.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            AddHandler device.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
            AddHandler device.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            AddHandler device.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            AddHandler device.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
        End Sub
    
        ''' <summary>
        ''' Unsubscribes from the device events.
        ''' </summary>
        Private Shared Sub UnsubscribeFromDeviceEvents(ByVal device As Vintasoft.Twain.Device)
            RemoveHandler device.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
            RemoveHandler device.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
        End Sub
    
    End Class
    
    


    Вот пример, который демонстрирует, как запустить синхронное получение изображения, остановить устройство подачи документов и продолжить синхронное получение изображения:
    namespace TwainExamples_CSharp
    {
        class SyncScan_StopFeeder
        {
    
            /// <summary>
            /// This method scans the first image, stops the document feeder,
            /// scans the second image.
            /// </summary>
            public static void ScanImage_StopFeeder_ScanImage()
            {
                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();
                }
            }
    
        }
    }
    
    
    Class SyncScan_StopFeeder
    
        ''' <summary>
        ''' This method scans the first image, stops the document feeder,
        ''' scans the second image.
        ''' </summary>
        Public Shared Sub ScanImage_StopFeeder_ScanImage()
            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 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 synchronous image acquisition
                Dim acquireModalState1 As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
                Do
                    acquireModalState1 = device.AcquireModal()
                    Select Case acquireModalState1
                        Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                            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
                            Console.WriteLine("Scan is completed.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                            Console.WriteLine("Scan is canceled.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanFailed
                            Console.WriteLine(String.Format("Scan is failed: {0}.", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState1 <> Vintasoft.Twain.AcquireModalState.None
    
                If imageCount <> 1 Then
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                If device.State <> Vintasoft.Twain.DeviceState.TransferReady Then
                    Throw New ApplicationException("Feeder is NOT stopped.")
                End If
                Console.WriteLine("Feeder is stopped.")
    
                ' continue asynchronous image acquisition
                Do
                    acquireModalState1 = device.AcquireModal()
                    Select Case acquireModalState1
                        Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                            Console.WriteLine("Image is acquired.")
    
                            If device.AcquiredImage IsNot Nothing Then
                                device.AcquiredImage.Dispose()
                            End If
    
                            imageCount = imageCount + 1
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCompleted
                            Console.WriteLine("Scan is completed.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                            Console.WriteLine("Scan is canceled.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanFailed
                            Console.WriteLine(String.Format("Scan is failed: {0}.", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState1 <> Vintasoft.Twain.AcquireModalState.None
    
                If imageCount <> 2 Then
                    Throw New ApplicationException("Wrong acquired image count.")
                End If
    
                ' close the device
                device.Close()
            End Using
        End Sub
    
    End Class