VintaSoft Twain .NET如何在扫描时跳过空白页的扫描

作者:控件中国网   出处:控件中国网   2016-06-21 14:13:05   阅读:12

当进行多页扫描时,如果发现扫描页中有空白页,那么怎么跳过该空白页不进行扫描呢,开发人员利用VintaSoft Twain .NET扫描控件可以轻松实现该功能,实现方法一般分两种,一种是利用扫描仪自带的功能,如果扫描仪支持 ICAP_AUTODISCARDBLANKPAGES能力就可以利用该功能设置不扫描空白页,如果扫描仪不具备该功能那么还可以利用VintaSoft Twain .NET控件提供的API来实现该功能,具体实现方法可以参考下面的详细代码:
 
1.使用扫描仪自带的ICAP_AUTODISCARDBLANKPAGES能力来跳过空白页的扫描
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<ImageAcquiredEventArgs>(device_ImageAcquired);
                device.ScanCanceled += new EventHandler(device_ScanCanceled);
                device.ScanFailed += new EventHandler<ScanFailedEventArgs>(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<ImageAcquiredEventArgs>(device_ImageAcquired);
            device.ScanCanceled -= new EventHandler(device_ScanCanceled);
            device.ScanFailed -= new EventHandler<ScanFailedEventArgs>(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.");
        }
    }
}
2.使用VintaSoft Twain .NET扫描控件来实现跳过空白页的扫描
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<ImageAcquiredEventArgs>(device_ImageAcquired);
                device.ScanCanceled += new EventHandler(device_ScanCanceled);
                device.ScanFailed += new EventHandler<ScanFailedEventArgs>(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<ImageAcquiredEventArgs>(device_ImageAcquired);
            device.ScanCanceled -= new EventHandler(device_ScanCanceled);
            device.ScanFailed -= new EventHandler<ScanFailedEventArgs>(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.");
        }
    }
}
 
Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat