VintaSoft Twain .NET SDK v10.2
Acquire Method (Device)
Example 




Vintasoft.Twain Namespace > Device Class : Acquire Method
Acquires image(s) from the device asynchronously.
Syntax
'Declaration
 
Public Sub Acquire() 
'Usage
 
Dim instance As Device
 
instance.Acquire()
public void Acquire()
public: void Acquire(); 
public:
void Acquire(); 
Exceptions
ExceptionDescription
Thrown if device is disposed.
Thrown if device is in wrong state or device is not set as current device in device manager.
Thrown if the .NET debugger is used and the function evaluation requires all threads to run.
Example
This example shows how to asynchronously acquire images from the device.
Imports System.IO
Imports System.Windows.Forms
Imports Vintasoft.Twain
            
Partial Public Class ScanAsynchronouslyDemoForm
    Inherits Form
            
#Region "Fields"
            
    ' TWAIN device manager.
    Private _deviceManager As DeviceManager
            
    ' Current TWAIN device.
    Private _currentDevice As Device
            
#End Region
            
            
            
#Region "Constructors"
            
    Public Sub New()
        InitializeComponent()
            
        ' create and open device manager
        _deviceManager = New DeviceManager(Me)
        _deviceManager.Open()
    End Sub
            
#End Region
            
            
            
#Region "Methods"
            
    ' Start image acquisition from scanner.
    Private Sub startImageAcquisitionButton_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            ' select the default device
            _deviceManager.ShowDefaultDeviceSelectionDialog()
            
            ' get reference to the default device
            _currentDevice = _deviceManager.DefaultDevice
            
            ' subscribe to the device events
            AddHandler _currentDevice.ImageAcquiringProgress, New EventHandler(Of ImageAcquiringProgressEventArgs)(AddressOf device_ImageAcquiringProgress)
            AddHandler _currentDevice.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            AddHandler _currentDevice.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
            AddHandler _currentDevice.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            AddHandler _currentDevice.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
            AddHandler _currentDevice.UserInterfaceClosed, New EventHandler(AddressOf device_UserInterfaceClosed)
            AddHandler _currentDevice.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
            
            ' set Memory transfer mode
            _currentDevice.TransferMode = TransferMode.Memory
            ' enable UI
            _currentDevice.ShowUI = True
            
            ' open the device
            _currentDevice.Open()
            ' specify that black-white images must be acquired
            _currentDevice.PixelType = PixelType.BW
            ' specify the threshold for acquired images
            _currentDevice.Threshold = 128
            ' set the inches as unit of measure
            _currentDevice.UnitOfMeasure = UnitOfMeasure.Inches
            _currentDevice.Resolution = New Resolution(300.0F, 300.0F)
            
            ' acquire images asynchronously
            _currentDevice.Acquire()
        Catch ex As TwainException
            MessageBox.Show(ex.Message)
        End Try
    End Sub
            
    ' Image acquisition is in progress.
    Private Sub device_ImageAcquiringProgress(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiringProgressEventArgs)
        ' update progress bar
        progressBar1.Value = e.Progress
    End Sub
            
    ' Image is acquired.
    Private Sub device_ImageAcquired(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
        ' dispose image stored in the picture box
        If pictureBox1.Image IsNot Nothing Then
            pictureBox1.Image.Dispose()
            pictureBox1.Image = Nothing
        End If
            
        ' show acquired image in the picture box
        pictureBox1.Image = e.Image.GetAsBitmap(True)
            
        ' add the acquired image to a TIFF file
        e.Image.Save(Path.Combine(Directory.GetCurrentDirectory(), "multipage.tif"))
            
        ' dispose the acquired image
        e.Image.Dispose()
    End Sub
            
    ' Scan is completed.
    Private Sub device_ScanCompleted(ByVal sender As Object, ByVal e As EventArgs)
        MessageBox.Show("Scan is competed.")
    End Sub
            
    ' Scan is canceled.
    Private Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
        MessageBox.Show("Scan is canceled.")
    End Sub
            
    ' Scan is failed.
    Private Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
        MessageBox.Show(String.Format("Scan is failed: {0}", e.ErrorString))
    End Sub
            
    ' User interface of device is closed.
    Private Sub device_UserInterfaceClosed(ByVal sender As Object, ByVal e As EventArgs)
        MessageBox.Show("User Interface is closed.")
    End Sub
            
    ' Scan is finished.
    Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
        Dim device1 As Device = DirectCast(sender, Device)
            
        ' unsubscribe from device events
        RemoveHandler _currentDevice.ImageAcquiringProgress, New EventHandler(Of ImageAcquiringProgressEventArgs)(AddressOf device_ImageAcquiringProgress)
        RemoveHandler _currentDevice.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
        RemoveHandler _currentDevice.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
        RemoveHandler _currentDevice.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
        RemoveHandler _currentDevice.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
        RemoveHandler _currentDevice.UserInterfaceClosed, New EventHandler(AddressOf device_UserInterfaceClosed)
        RemoveHandler _currentDevice.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
            
        ' if device is not closed
        If _currentDevice.State <> DeviceState.Closed Then
            ' close the device
            _currentDevice.Close()
        End If
            
        MessageBox.Show("Scan is finished.")
    End Sub
            
#End Region
            
End Class
using System;
using System.IO;
using System.Windows.Forms;
using Vintasoft.Twain;
            
namespace TwainExamples_CSharp
{
    public partial class ScanAsynchronouslyDemoForm : Form
    {
            
        #region Fields
            
        // TWAIN device manager.
        DeviceManager _deviceManager;
            
        // Current TWAIN device.
        Device _currentDevice;
            
        #endregion
            
            
            
        #region Constructors
            
        public ScanAsynchronouslyDemoForm()
        {
            InitializeComponent();
            
            // create and open device manager
            _deviceManager = new DeviceManager(this);
            _deviceManager.Open();
        }
            
        #endregion
            
            
            
        #region Methods
            
        // Start image acquisition from scanner.
        private void startImageAcquisitionButton_Click(object sender, EventArgs e)
        {
            try
            {
                // select the default device
                _deviceManager.ShowDefaultDeviceSelectionDialog();
            
                // get reference to the default device
                _currentDevice = _deviceManager.DefaultDevice;
            
                // subscribe to the device events
                _currentDevice.ImageAcquiringProgress += new EventHandler<ImageAcquiringProgressEventArgs>(device_ImageAcquiringProgress);
                _currentDevice.ImageAcquired += new EventHandler<ImageAcquiredEventArgs>(device_ImageAcquired);
                _currentDevice.ScanCompleted += new EventHandler(device_ScanCompleted);
                _currentDevice.ScanCanceled += new EventHandler(device_ScanCanceled);
                _currentDevice.ScanFailed += new EventHandler<ScanFailedEventArgs>(device_ScanFailed);
                _currentDevice.UserInterfaceClosed += new EventHandler(device_UserInterfaceClosed);
                _currentDevice.ScanFinished += new EventHandler(device_ScanFinished);
            
                // set Memory transfer mode
                _currentDevice.TransferMode = TransferMode.Memory;
                // enable UI
                _currentDevice.ShowUI = true;
                
                // open the device
                _currentDevice.Open();
                // specify that black-white images must be acquired
                _currentDevice.PixelType = PixelType.BW;
                // specify the threshold for acquired images
                _currentDevice.Threshold = 128;
                // set the inches as unit of measure
                _currentDevice.UnitOfMeasure = UnitOfMeasure.Inches;
                _currentDevice.Resolution = new Resolution(300f, 300f);
            
                // acquire images asynchronously
                _currentDevice.Acquire();
            }
            catch (TwainException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
            
        // Image acquisition is in progress.
        private void device_ImageAcquiringProgress(object sender, Vintasoft.Twain.ImageAcquiringProgressEventArgs e)
        {
            // update progress bar
            progressBar1.Value = e.Progress;
        }
            
        // Image is acquired.
        private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
        {
            // dispose image stored in the picture box
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = null;
            }
            
            // show acquired image in the picture box
            pictureBox1.Image = e.Image.GetAsBitmap(true);
            
            // add the acquired image to a TIFF file
            e.Image.Save(Path.Combine(Directory.GetCurrentDirectory(), "multipage.tif"));
            
            // dispose the acquired image
            e.Image.Dispose();
        }
            
        // Scan is completed.
        private void device_ScanCompleted(object sender, EventArgs e)
        {
            MessageBox.Show("Scan is competed.");
        }
            
        // Scan is canceled.
        private void device_ScanCanceled(object sender, EventArgs e)
        {
            MessageBox.Show("Scan is canceled.");
        }
            
        // Scan is failed.
        private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
        {
            MessageBox.Show(string.Format("Scan is failed: {0}", e.ErrorString));
        }
            
        // User interface of device is closed.
        private void device_UserInterfaceClosed(object sender, EventArgs e)
        {
            MessageBox.Show("User Interface is closed.");
        }
            
        // Scan is finished.
        void device_ScanFinished(object sender, EventArgs e)
        {
            Device device = (Device)sender;
            
            // unsubscribe from device events
            _currentDevice.ImageAcquiringProgress -= new EventHandler<ImageAcquiringProgressEventArgs>(device_ImageAcquiringProgress);
            _currentDevice.ImageAcquired -= new EventHandler<ImageAcquiredEventArgs>(device_ImageAcquired);
            _currentDevice.ScanCompleted -= new EventHandler(device_ScanCompleted);
            _currentDevice.ScanCanceled -= new EventHandler(device_ScanCanceled);
            _currentDevice.ScanFailed -= new EventHandler<ScanFailedEventArgs>(device_ScanFailed);
            _currentDevice.UserInterfaceClosed -= new EventHandler(device_UserInterfaceClosed);
            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.");
        }
            
        #endregion
            
    }
}
Requirements

Target Platforms: Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

See Also

Reference

Device Class
Device Members
AcquireModal Method
AcquiredImage Property
CancelTransfer Method
Close Method
DisableAfterAcquire Property
ShowUI Property
ShowIndicators Property