VintaSoft Twain .NET SDK v10.2
In This Topic
    How to skip blank pages while scanning?
    In This Topic

    Blank pages can be skipped while scanning by 2 ways:

    1. using the scanner capabilities
    2. using the library (SDK) features

    1. Skipping of blank pages using the scanner capabilities

    For the beginning, it is necessary to check if the scanner supports the ability to skip blank pages - check if the scanner supports ICAP_AUTODISCARDBLANKPAGES capability.

    If the scanner supports that capability, then you need to set the value of the capability in a way to let the scanner skip blank pages or not.
    Valid values of the capability:

    Here is an example showing how to skip blank pages if the scanner supports this ability:
    Imports System.Windows.Forms
    Imports Vintasoft.Twain
    
    Partial Public Class How_to_skip_blank_pages_using_ICAP_AUTODISCARDBLANKPAGES
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As DeviceManager
    
    
    
        Public Sub New()
            InitializeComponent()
    
            ' create and open device manager
            _deviceManager = New DeviceManager(Me)
            _deviceManager.Open()
        End Sub
    
    
    
        ''' <summary>
        ''' Acquire images asynchronously.
        ''' </summary>
        Public Sub AcquireImagesAsynchronously()
            Try
                ' get reference to the default device
                Dim device1 As Device = _deviceManager.DefaultDevice
    
                ' subscribe to the device events
                AddHandler device1.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
                AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
                AddHandler device1.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = TransferMode.Memory
                device1.ShowUI = False
    
                ' open the device
                device1.Open()
    
                ' get a reference to the IAutoDiscardBlankPages capability
                Dim autoDiscardBlankPagesCap As DeviceCapability = device1.Capabilities.Find(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 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 Device = DirectCast(sender, Device)
    
            ' unsubscribe from device events
            RemoveHandler device1.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device1.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
            ' if device is not closed
            If device1.State <> DeviceState.Closed Then
                ' close the device
                device1.Close()
            End If
    
            MessageBox.Show("Scan is finished.")
        End Sub
    
    End Class
                  
    
    using System;
    using System.Windows.Forms;
    using Vintasoft.Twain;
    
    namespace TwainExamples_CSharp
    {
        public partial class How_to_skip_blank_pages_using_ICAP_AUTODISCARDBLANKPAGES : Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            DeviceManager _deviceManager;
    
    
    
            public How_to_skip_blank_pages_using_ICAP_AUTODISCARDBLANKPAGES()
            {
                InitializeComponent();
    
                // create and open device manager
                _deviceManager = new DeviceManager(this);
                _deviceManager.Open();
            }
    
    
    
            /// <summary>
            /// Acquire images asynchronously.
            /// </summary>
            public void AcquireImagesAsynchronously()
            {
                try
                {
                    // get reference to the default device
                    Device device = _deviceManager.DefaultDevice;
    
                    // subscribe to the device events
                    device.ImageAcquired += new EventHandler(device_ImageAcquired);
                    device.ScanCanceled += new EventHandler(device_ScanCanceled);
                    device.ScanFailed += new EventHandler(device_ScanFailed);
                    device.ScanFinished += new EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = TransferMode.Memory;
                    device.ShowUI = false;
    
                    // open the device
                    device.Open();
    
                    // get a reference to the IAutoDiscardBlankPages capability
                    DeviceCapability autoDiscardBlankPagesCap = device.Capabilities.Find(DeviceCapabilityId.IAutoDiscardBlankPages);
                    // if discarding of blank pages is not supported
                    if (autoDiscardBlankPagesCap == null)
                        MessageBox.Show("Device cannot automatically discard blank pages.");
                    else
                        // enable discarding of blank pages
                        autoDiscardBlankPagesCap.SetValue(true);
    
                    // acquire images asynchronously
                    device.Acquire();
                }
                catch (TwainException ex)
                {
                    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, EventArgs e)
            {
                MessageBox.Show("Scan is canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
            {
                MessageBox.Show("Scan is failed: " + e.ErrorString);
            }
    
            /// <summary>
            /// Scan is finished.
            /// </summary>
            void device_ScanFinished(object sender, EventArgs e)
            {
                Device device = (Device)sender;
    
                // unsubscribe from device events
                device.ImageAcquired -= new EventHandler(device_ImageAcquired);
                device.ScanCanceled -= new EventHandler(device_ScanCanceled);
                device.ScanFailed -= new EventHandler(device_ScanFailed);
                device.ScanFinished -= new EventHandler(device_ScanFinished);
    
                // if device is not closed
                if (device.State != DeviceState.Closed)
                    // close the device
                    device.Close();
    
                MessageBox.Show("Scan is finished.");
            }
    
        }
    }
                    
    


    Advantages of this technique:
    Disadvantages of this technique:


    2. Skipping of blank pages using the library (SDK) features

    The algorithm of this technique is quite simple. The scanner sends to the library all images of acquired pages and the library checks if the image is blank using AcquiredImage.IsBlank method.

    Here is an example showing how to skip blank pages using IsBlank method:
    Imports System.Windows.Forms
    Imports Vintasoft.Twain
    
    Partial Public Class How_to_skip_blank_pages_using_IsBlank
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As DeviceManager
    
    
    
        Public Sub New()
            InitializeComponent()
    
            ' create and open device manager
            _deviceManager = New DeviceManager(Me)
            _deviceManager.Open()
        End Sub
    
    
    
        ''' <summary>
        ''' Acquire images asynchronously.
        ''' </summary>
        Public Sub AcquireImagesAsynchronously()
            Try
                ' get reference to the default device
                Dim device1 As Device = _deviceManager.DefaultDevice
    
                ' subscribe to the device events
                AddHandler device1.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
                AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
                AddHandler device1.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = TransferMode.Memory
                device1.ShowUI = False
    
                ' acquire images asynchronously
                device1.Acquire()
            Catch ex As 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 Device = DirectCast(sender, Device)
    
            ' unsubscribe from device events
            RemoveHandler device1.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device1.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
            ' if device is not closed
            If device1.State <> DeviceState.Closed Then
                ' close the device
                device1.Close()
            End If
    
            MessageBox.Show("Scan is finished.")
        End Sub
    
    End Class
                  
    
    using System;
    using System.Windows.Forms;
    using Vintasoft.Twain;
    
    namespace TwainExamples_CSharp
    {
        public partial class How_to_skip_blank_pages_using_IsBlank : Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            DeviceManager _deviceManager;
    
    
    
            public How_to_skip_blank_pages_using_IsBlank()
            {
                InitializeComponent();
    
                // create and open device manager
                _deviceManager = new DeviceManager(this);
                _deviceManager.Open();
            }
    
    
    
            /// <summary>
            /// Acquire images asynchronously.
            /// </summary>
            public void AcquireImagesAsynchronously()
            {
                try
                {
                    // get reference to the default device
                    Device device = _deviceManager.DefaultDevice;
    
                    // subscribe to the device events
                    device.ImageAcquired += new EventHandler(device_ImageAcquired);
                    device.ScanCanceled += new EventHandler(device_ScanCanceled);
                    device.ScanFailed += new EventHandler(device_ScanFailed);
                    device.ScanFinished += new EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = TransferMode.Memory;
                    device.ShowUI = false;
    
                    // acquire images asynchronously
                    device.Acquire();
                }
                catch (TwainException ex)
                {
                    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, EventArgs e)
            {
                MessageBox.Show("Scan canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
            {
                MessageBox.Show("Scan failed: " + e.ErrorString);
            }
    
            /// <summary>
            /// Scan is finished.
            /// </summary>
            void device_ScanFinished(object sender, EventArgs e)
            {
                Device device = (Device)sender;
    
                // unsubscribe from device events
                device.ImageAcquired -= new EventHandler(device_ImageAcquired);
                device.ScanCanceled -= new EventHandler(device_ScanCanceled);
                device.ScanFailed -= new EventHandler(device_ScanFailed);
                device.ScanFinished -= new EventHandler(device_ScanFinished);
    
                // if device is not closed
                if (device.State != DeviceState.Closed)
                    // close the device
                    device.Close();
    
                MessageBox.Show("Scan is finished.");
            }
    
        }
    }
                    
    


    Advantages of this technique:
    Disadvantages of this technique: