How to skip blank pages while scanning?
In This Topic
Blank pages can be skipped while TWAIN image scanning by 2 ways:
- using TWAIN scanner capabilities
- using the library (SDK) features
1. Skipping of blank pages using TWAIN scanner capabilities
For the beginning, it is necessary to check if TWAIN scanner supports the ability to skip blank pages - check if TWAIN scanner supports ICAP_AUTODISCARDBLANKPAGES capability.
If TWAIN scanner supports that capability, then you need to set the value of the capability in a way to let TWAIN scanner skip blank pages or not.
Valid values of the capability:
- -2 - TWAIN scanner must NOT skip blank pages while scanning
- -1 - TWAIN scanner must skip blank pages while scanning
Here is an example that demonstrates how to use TWAIN scanner capability skipping blank pages during TWAIN image scanning:
/// <summary>
/// Synchronously acquires images from TWAIN device and skips blank pages using TWAIN capability.
/// </summary>
public void SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingTwainCapability()
{
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;
// 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);
// synchronously acquire image(s) from TWAIN device
Vintasoft.Twain.AcquireModalState acquireModalState;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// add the acquired image to a TIFF file
device.AcquiredImage.Save(@"d:\test.tif");
// dispose the acquired image
device.AcquiredImage.Dispose();
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 acquires images from TWAIN device and skips blank pages using TWAIN capability.
''' </summary>
Public Sub SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingTwainCapability()
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
' set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory
device.ShowUI = False
' open the device
device.Open()
' get a reference to the IAutoDiscardBlankPages capability
Dim autoDiscardBlankPagesCap As Vintasoft.Twain.DeviceCapability = device.Capabilities.Find(Vintasoft.Twain.DeviceCapabilityId.IAutoDiscardBlankPages)
' if discarding of blank pages is not supported
If autoDiscardBlankPagesCap Is Nothing Then
System.Windows.Forms.MessageBox.Show("Device cannot automatically discard blank pages.")
Else
' enable discarding of blank pages
autoDiscardBlankPagesCap.SetValue(True)
End If
' synchronously acquire image(s) from TWAIN device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' add the acquired image to a TIFF file
device.AcquiredImage.Save("d:\test.tif")
' dispose the acquired image
device.AcquiredImage.Dispose()
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
Advantages of this technique:
- When TWAIN scanner skips blank page while scanning - it doesn't send the page to the application and thus saves the memory usage.
Disadvantages of this technique:
- Only the professional, rather expensive, TWAIN scanners support skipping of blank pages.
2. Skipping of blank pages using the library (SDK) features
The algorithm of this technique is quite simple. TWAIN 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 that demonstrates how to use IsBlank method for skipping blank pages during TWAIN image scanning:
/// <summary>
/// Synchronously acquires images from TWAIN device and skips blank pages using AcquiredImage.IsBlank method.
/// </summary>
public void SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingIsBlankMethod()
{
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;
// set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
device.ShowUI = false;
// open the device
device.Open();
// synchronously acquire image(s) from TWAIN device
Vintasoft.Twain.AcquireModalState acquireModalState;
do
{
acquireModalState = device.AcquireModal();
switch (acquireModalState)
{
case Vintasoft.Twain.AcquireModalState.ImageAcquired:
// if acquired image is not blank
if (!device.AcquiredImage.IsBlank(0.01f))
// add the acquired image to a TIFF file
device.AcquiredImage.Save(@"d:\test.tif");
// dispose the acquired image
device.AcquiredImage.Dispose();
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 acquires images from TWAIN device and skips blank pages using AcquiredImage.IsBlank method.
''' </summary>
Public Sub SynchronouslyAcquireImagesFromTwainDeviceAndSkipBlankPagesUsingIsBlankMethod()
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
' set scanning settings
device.TransferMode = Vintasoft.Twain.TransferMode.Memory
device.ShowUI = False
' open the device
device.Open()
' synchronously acquire image(s) from TWAIN device
Dim acquireModalState As Vintasoft.Twain.AcquireModalState
Do
acquireModalState = device.AcquireModal()
Select Case acquireModalState
Case Vintasoft.Twain.AcquireModalState.ImageAcquired
' if acquired image is not blank
If Not device.AcquiredImage.IsBlank(0.01F) Then
' add the acquired image to a TIFF file
device.AcquiredImage.Save("d:\test.tif")
End If
' dispose the acquired image
device.AcquiredImage.Dispose()
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
Advantages of this technique:
- This works with every TWAIN scanner.
Disadvantages of this technique:
- Memory usage is greater than in first technique.