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




Vintasoft.Twain Namespace > Device Class : AcquireModal Method
Acquires image(s) from the device synchronously.
Syntax

Return Value

State of the modal acquisition process.
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.
Remarks
Important: This method must be called in a loop while it does not return the AcquireModalState.None value.
Example
This example shows how to synchronously acquire images from the device.
Public Shared Sub AcquireImageModal()
    Try
        ' create the device manager
        Using deviceManager As New DeviceManager()
            ' open the device manager
            deviceManager.Open()
            
            ' get reference to the default device
            Dim device As Device = deviceManager.DefaultDevice
            
            device.ShowUI = False
            device.DisableAfterAcquire = True
            
            ' open the device
            device.Open()
            
            ' set acquisition parameters
            device.TransferMode = TransferMode.Memory
            device.PixelType = PixelType.BW
            
            ' create directory for TIFF file
            Dim directoryForImages As String = Path.GetDirectoryName(Directory.GetCurrentDirectory())
            directoryForImages = Path.Combine(directoryForImages, "Images")
            If Not Directory.Exists(directoryForImages) Then
                Directory.CreateDirectory(directoryForImages)
            End If
            
            Dim multipageTiffFilename As String = Path.Combine(directoryForImages, "multipage.tif")
            
            ' acquire image(s) from the device
            Dim acquireModalState1 As AcquireModalState = AcquireModalState.None
            Dim imageIndex As Integer = 0
            Do
                acquireModalState1 = device.AcquireModal()
                Select Case acquireModalState1
                    Case AcquireModalState.ImageAcquiring
                        ' output current state
                        Console.Write(String.Format("Image is acquiring ", System.Math.Max(System.Threading.Interlocked.Increment(imageIndex), imageIndex - 1)))
                        Exit Select
            
                    Case AcquireModalState.ImageAcquiringProgress
                        ' output current state
                        Console.Write(String.Format("{0}% ", device.ImageAcquiringProgressValue))
                        Exit Select
            
                    Case AcquireModalState.ImageAcquired
                        ' save the acquired image to a file
                        device.AcquiredImage.Save(multipageTiffFilename)
                        ' dispose the acquired image
                        device.AcquiredImage.Dispose()
                        ' output current state
                        Console.WriteLine(String.Format("Image{0} is saved.", System.Math.Max(System.Threading.Interlocked.Increment(imageIndex), imageIndex - 1)))
                        Exit Select
            
                    Case AcquireModalState.ScanCompleted
                        ' output current state
                        Console.WriteLine("Scan is completed.")
                        Exit Select
            
                    Case AcquireModalState.ScanCanceled
                        ' output current state
                        Console.WriteLine("Scan is canceled.")
                        Exit Select
            
                    Case AcquireModalState.ScanFailed
                        ' output current state
                        Console.WriteLine(String.Format("Scan is failed: {0}", device.ErrorString))
                        Exit Select
                End Select
            Loop While acquireModalState1 <> AcquireModalState.None
            
            ' close the device
            device.Close()
            
            ' close the device manager
            deviceManager.Close()
        End Using
    Catch ex As TwainException
        Console.WriteLine("Error: " + ex.Message)
    End Try
End Sub
public static void AcquireImageModal()
{
    try
    {
        // create the device manager
        using (DeviceManager deviceManager = new DeviceManager())
        {
            // open the device manager
            deviceManager.Open();
            
            // get reference to the default device
            Device device = deviceManager.DefaultDevice;
            
            device.ShowUI = false;
            device.DisableAfterAcquire = true;
            
            // open the device
            device.Open();
            
            // set acquisition parameters
            device.TransferMode = TransferMode.Memory;
            device.PixelType = PixelType.BW;
            
            // create directory for TIFF file
            string directoryForImages = Path.GetDirectoryName(Directory.GetCurrentDirectory());
            directoryForImages = Path.Combine(directoryForImages, "Images");
            if (!Directory.Exists(directoryForImages))
                Directory.CreateDirectory(directoryForImages);
            
            string multipageTiffFilename = Path.Combine(directoryForImages, "multipage.tif");
            
            // acquire image(s) from the device
            AcquireModalState acquireModalState = AcquireModalState.None;
            int imageIndex = 0;
            do
            {
                acquireModalState = device.AcquireModal();
                switch (acquireModalState)
                {
                    case AcquireModalState.ImageAcquiring:
                        // output current state
                        Console.Write(string.Format("Image is acquiring ", imageIndex++));
                        break;
            
                    case AcquireModalState.ImageAcquiringProgress:
                        // output current state
                        Console.Write(string.Format("{0}% ", device.ImageAcquiringProgressValue));
                        break;
            
                    case AcquireModalState.ImageAcquired:
                        // save the acquired image to a file
                        device.AcquiredImage.Save(multipageTiffFilename);
                        // dispose the acquired image
                        device.AcquiredImage.Dispose();
                        // output current state
                        Console.WriteLine(string.Format("Image{0} is saved.", imageIndex++));
                        break;
            
                    case AcquireModalState.ScanCompleted:
                        // output current state
                        Console.WriteLine("Scan is completed.");
                        break;
            
                    case AcquireModalState.ScanCanceled:
                        // output current state
                        Console.WriteLine("Scan is canceled.");
                        break;
            
                    case AcquireModalState.ScanFailed:
                        // output current state
                        Console.WriteLine(string.Format("Scan is failed: {0}", device.ErrorString));
                        break;
                }
            }
            while (acquireModalState != AcquireModalState.None);
            
            // close the device
            device.Close();
            
            // close the device manager
            deviceManager.Close();
        }
    }
    catch (TwainException ex)
    {
        Console.WriteLine("Error: " + ex.Message);
    }
}
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
Acquire Method
AcquiredImage Property
ImageAcquiringProgressValue Property
AsyncEventArgs Property
CancelTransfer Method
Close Method
ErrorString Property
DisableAfterAcquire Property
ShowIndicators Property
ShowUI Property