VintaSoft Twain .NET SDK v10.2
Device Class
Members  Example 




Vintasoft.Twain Namespace : Device Class
Class that allows to manipulate TWAIN device.
Object Model
Device ClassAcquiredImage ClassDeviceAsyncEventArgs ClassDeviceEventId EnumerationDeviceCameraCollection ClassDeviceCapabilityCollection ClassDeviceCapability ClassDocumentFeeder ClassExtendedImageInfoCollection ClassExtendedImageInfo ClassDeviceImageLayout ClassDeviceInfo ClassResolution Class
Syntax
'Declaration
 
<LicenseProviderAttribute(Vintasoft.Twain.VintasoftLicenseProvider)>
Public NotInheritable Class Device 
'Usage
 
Dim instance As Device
[LicenseProvider(Vintasoft.Twain.VintasoftLicenseProvider)]
public sealed class Device 
[LicenseProvider(Vintasoft.Twain.VintasoftLicenseProvider)]
public __gc __sealed class Device 
[LicenseProvider(Vintasoft.Twain.VintasoftLicenseProvider)]
public ref class Device sealed 
Remarks
Class allows to:
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
            
    }
}
Inheritance Hierarchy

System.Object
   Vintasoft.Twain.Device

Requirements

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

See Also

Reference

Device Members
Vintasoft.Twain Namespace