VintaSoft Twain .NET SDK 14.1: Руководство для .NET разработчика
В этом разделе
    Как пропустить пустые страницы при TWAIN-сканировании?
    В этом разделе
    Пустые страницы можно пропускать при сканировании двумя способами:
    1. используя возможности сканера TWAIN
    2. используя возможности библиотеки (SDK)

    1. Пропуск пустых страниц при использовании возможностей сканера TWAIN

    Для начала необходимо проверить, поддерживает ли TWAIN-сканер возможность пропуска пустых страниц - проверьте, поддерживает ли TWAIN-сканер функцию ICAP_AUTODISCARDBLANKPAGES.

    Если TWAIN-сканер поддерживает эту возможность, то вам нужно установить значение этой функции таким образом, чтобы TWAIN-сканер пропускал пустые страницы или нет.
    Допустимые значения данной функции:

    Вот пример, который демонстрирует, как пропускать пустые страницы, если TWAIN-сканер поддерживает эту опцию:
    namespace TwainExamples_CSharp
    {
        public partial class How_to_skip_blank_pages_using_ICAP_AUTODISCARDBLANKPAGES : System.Windows.Forms.Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            Vintasoft.Twain.DeviceManager _deviceManager;
    
    
    
            public How_to_skip_blank_pages_using_ICAP_AUTODISCARDBLANKPAGES()
            {
                InitializeComponent();
    
                // create and open device manager
                _deviceManager = new Vintasoft.Twain.DeviceManager(this);
                _deviceManager.Open();
            }
    
    
    
            /// <summary>
            /// Acquire images asynchronously.
            /// </summary>
            public void AcquireImagesAsynchronously()
            {
                try
                {
                    // get reference to the default device
                    Vintasoft.Twain.Device device = _deviceManager.DefaultDevice;
    
                    // subscribe to the device events
                    device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
                    device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
                    device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
                    device.ScanFinished += new System.EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                    device.ShowUI = false;
    
                    // open the device
                    device.Open();
    
                    // get a reference to the IAutoDiscardBlankPages capability
                    Vintasoft.Twain.DeviceCapability autoDiscardBlankPagesCap = device.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IAutoDiscardBlankPages);
                    // if discarding of blank pages is not supported
                    if (autoDiscardBlankPagesCap == null)
                        System.Windows.Forms.MessageBox.Show("Device cannot automatically discard blank pages.");
                    else
                        // enable discarding of blank pages
                        autoDiscardBlankPagesCap.SetValue(true);
    
                    // acquire images asynchronously
                    device.Acquire();
                }
                catch (Vintasoft.Twain.TwainException ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
    
            /// <summary>
            /// Image is acquired.
            /// </summary>
            private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
            {
                // add the acquired image to a TIFF file
                e.Image.Save(@"d:\test.tif");
    
                // dispose the acquired image
                e.Image.Dispose();
            }
    
            /// <summary>
            /// Scan is canceled.
            /// </summary>
            private void device_ScanCanceled(object sender, System.EventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Scan is canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Scan is failed: " + e.ErrorString);
            }
    
            /// <summary>
            /// Scan is finished.
            /// </summary>
            void device_ScanFinished(object sender, System.EventArgs e)
            {
                Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
    
                // unsubscribe from device events
                device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
                device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
                device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
                device.ScanFinished -= new System.EventHandler(device_ScanFinished);
    
                // if device is not closed
                if (device.State != Vintasoft.Twain.DeviceState.Closed)
                    // close the device
                    device.Close();
    
                System.Windows.Forms.MessageBox.Show("Scan is finished.");
            }
    
        }
    }
    
    
    Partial Public Class How_to_skip_blank_pages_using_ICAP_AUTODISCARDBLANKPAGES
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As Vintasoft.Twain.DeviceManager
    
    
    
        Public Sub New()
            InitializeComponent()
    
            ' create and open device manager
            _deviceManager = New Vintasoft.Twain.DeviceManager(Me)
            _deviceManager.Open()
        End Sub
    
    
    
        ''' <summary>
        ''' Acquire images asynchronously.
        ''' </summary>
        Public Sub AcquireImagesAsynchronously()
            Try
                ' get reference to the default device
                Dim device1 As Vintasoft.Twain.Device = _deviceManager.DefaultDevice
    
                ' subscribe to the device events
                AddHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
                AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
                AddHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device1.ShowUI = False
    
                ' open the device
                device1.Open()
    
                ' get a reference to the IAutoDiscardBlankPages capability
                Dim autoDiscardBlankPagesCap As Vintasoft.Twain.DeviceCapability = device1.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IAutoDiscardBlankPages)
                ' if discarding of blank pages is not supported
                If autoDiscardBlankPagesCap Is Nothing Then
                    MessageBox.Show("Device cannot automatically discard blank pages.")
                Else
                    ' enable discarding of blank pages
                    autoDiscardBlankPagesCap.SetValue(True)
                End If
    
                ' acquire images asynchronously
                device1.Acquire()
            Catch ex As Vintasoft.Twain.TwainException
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        ''' <summary>
        ''' Image is acquired.
        ''' </summary>
        Private Sub device_ImageAcquired(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
            ' add the acquired image to a TIFF file
            e.Image.Save("d:\test.tif")
    
            ' dispose the acquired image
            e.Image.Dispose()
        End Sub
    
        ''' <summary>
        ''' Scan is canceled.
        ''' </summary>
        Private Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
            MessageBox.Show("Scan is canceled.")
        End Sub
    
        ''' <summary>
        ''' Scan is failed.
        ''' </summary>
        Private Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
            MessageBox.Show("Scan is failed: " + Convert.ToString(e.ErrorString))
        End Sub
    
        ''' <summary>
        ''' Scan is finished.
        ''' </summary>
        Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            Dim device1 As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
    
            ' unsubscribe from device events
            RemoveHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
            ' if device is not closed
            If device1.State <> Vintasoft.Twain.DeviceState.Closed Then
                ' close the device
                device1.Close()
            End If
    
            MessageBox.Show("Scan is finished.")
        End Sub
    
    End Class
    
    


    Преимущества этой техники:

    Недостатки этой техники:


    2. Пропуск пустых страниц с использованием возможностей библиотеки (SDK)

    Алгоритм этой техники довольно прост. TWAIN-сканер отправляет в библиотеку все изображения полученных страниц, а библиотека проверяет, является ли изображение пустым, используя метод Vintasoft.Twain.AcquiredImage.IsBlank.

    Вот пример, который демонстрирует, как пропускать пустые страницы с помощью метода IsBlank:
    namespace TwainExamples_CSharp
    {
        public partial class How_to_skip_blank_pages_using_IsBlank : System.Windows.Forms.Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            Vintasoft.Twain.DeviceManager _deviceManager;
    
    
    
            public How_to_skip_blank_pages_using_IsBlank()
            {
                InitializeComponent();
    
                // create and open device manager
                _deviceManager = new Vintasoft.Twain.DeviceManager(this);
                _deviceManager.Open();
            }
    
    
    
            /// <summary>
            /// Acquire images asynchronously.
            /// </summary>
            public void AcquireImagesAsynchronously()
            {
                try
                {
                    // get reference to the default device
                    Vintasoft.Twain.Device device = _deviceManager.DefaultDevice;
    
                    // subscribe to the device events
                    device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
                    device.ScanCanceled += new System.EventHandler(device_ScanCanceled);
                    device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
                    device.ScanFinished += new System.EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                    device.ShowUI = false;
    
                    // acquire images asynchronously
                    device.Acquire();
                }
                catch (Vintasoft.Twain.TwainException ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
    
            /// <summary>
            /// Image is acquired.
            /// </summary>
            private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
            {
                // if acquired image is not blank
                if (!e.Image.IsBlank(0.01f))
                    // add the acquired image to a TIFF file
                    e.Image.Save(@"d:\test.tif");
    
                // dispose the acquired image
                e.Image.Dispose();
            }
    
            /// <summary>
            /// Scan is canceled.
            /// </summary>
            private void device_ScanCanceled(object sender, System.EventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Scan canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Scan failed: " + e.ErrorString);
            }
    
            /// <summary>
            /// Scan is finished.
            /// </summary>
            void device_ScanFinished(object sender, System.EventArgs e)
            {
                Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
    
                // unsubscribe from device events
                device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
                device.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
                device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
                device.ScanFinished -= new System.EventHandler(device_ScanFinished);
    
                // if device is not closed
                if (device.State != Vintasoft.Twain.DeviceState.Closed)
                    // close the device
                    device.Close();
    
                System.Windows.Forms.MessageBox.Show("Scan is finished.");
            }
    
        }
    }
    
    
    Partial Public Class How_to_skip_blank_pages_using_IsBlank
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As Vintasoft.Twain.DeviceManager
    
    
    
        Public Sub New()
            InitializeComponent()
    
            ' create and open device manager
            _deviceManager = New Vintasoft.Twain.DeviceManager(Me)
            _deviceManager.Open()
        End Sub
    
    
    
        ''' <summary>
        ''' Acquire images asynchronously.
        ''' </summary>
        Public Sub AcquireImagesAsynchronously()
            Try
                ' get reference to the default device
                Dim device1 As Vintasoft.Twain.Device = _deviceManager.DefaultDevice
    
                ' subscribe to the device events
                AddHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
                AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
                AddHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device1.ShowUI = False
    
                ' acquire images asynchronously
                device1.Acquire()
            Catch ex As Vintasoft.Twain.TwainException
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        ''' <summary>
        ''' Image is acquired.
        ''' </summary>
        Private Sub device_ImageAcquired(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
            ' if acquired image is not blank
            If Not e.Image.IsBlank(0.01F) Then
                ' add the acquired image to a TIFF file
                e.Image.Save("d:\test.tif")
            End If
    
            ' dispose the acquired image
            e.Image.Dispose()
        End Sub
    
        ''' <summary>
        ''' Scan is canceled.
        ''' </summary>
        Private Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
            MessageBox.Show("Scan is canceled.")
        End Sub
    
        ''' <summary>
        ''' Scan is failed.
        ''' </summary>
        Private Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
            MessageBox.Show("Scan is failed: " + Convert.ToString(e.ErrorString))
        End Sub
    
        ''' <summary>
        ''' Scan is finished.
        ''' </summary>
        Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            Dim device1 As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
    
            ' unsubscribe from device events
            RemoveHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
            ' if device is not closed
            If device1.State <> Vintasoft.Twain.DeviceState.Closed Then
                ' close the device
                device1.Close()
            End If
    
            MessageBox.Show("Scan is finished.")
        End Sub
    
    End Class
    
    


    Преимущества этой техники:

    Недостатки этой техники: