VintaSoft Twain .NET SDK 14.1: Documentation for .NET developer
Vintasoft.Twain Namespace / Device Class / AcquireModal() Method
Syntax Exceptions Remarks Example Requirements SeeAlso
In This Topic
    AcquireModal() Method (Device)
    In This Topic
    Acquires image(s) from the device synchronously.
    Syntax
    'Declaration
    
    Public Function AcquireModal() As AcquireModalState
    
    
    public AcquireModalState AcquireModal()
    
    
    public: AcquireModalState AcquireModal()
    
    
    public:
    AcquireModalState AcquireModal()

    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 C#/VB.NET code shows how to synchronously acquire images from the device.

    
    Public Shared Sub AcquireImageModal()
        Try
            ' create the device manager
            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
    
                device.ShowUI = False
                device.DisableAfterAcquire = True
    
                ' open the device
                device.Open()
    
                ' set acquisition parameters
                device.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device.PixelType = Vintasoft.Twain.PixelType.BW
    
                ' create directory for TIFF file
                Dim directoryForImages As String = IO.Path.GetDirectoryName(IO.Directory.GetCurrentDirectory())
                directoryForImages = IO.Path.Combine(directoryForImages, "Images")
                If Not IO.Directory.Exists(directoryForImages) Then
                    IO.Directory.CreateDirectory(directoryForImages)
                End If
    
                Dim multipageTiffFilename As String = IO.Path.Combine(directoryForImages, "multipage.tif")
    
                ' acquire image(s) from the device
                Dim acquireModalState1 As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
                Dim imageIndex As Integer = 0
                Do
                    acquireModalState1 = device.AcquireModal()
                    Select Case acquireModalState1
                        Case Vintasoft.Twain.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 Vintasoft.Twain.AcquireModalState.ImageAcquiringProgress
                            ' output current state
                            Console.Write(String.Format("{0}% ", device.ImageAcquiringProgressValue))
                            Exit Select
    
                        Case Vintasoft.Twain.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 Vintasoft.Twain.AcquireModalState.ScanCompleted
                            ' output current state
                            Console.WriteLine("Scan is completed.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanCanceled
                            ' output current state
                            Console.WriteLine("Scan is canceled.")
                            Exit Select
    
                        Case Vintasoft.Twain.AcquireModalState.ScanFailed
                            ' output current state
                            Console.WriteLine(String.Format("Scan is failed: {0}", device.ErrorString))
                            Exit Select
                    End Select
                Loop While acquireModalState1 <> Vintasoft.Twain.AcquireModalState.None
    
                ' close the device
                device.Close()
    
                ' close the device manager
                deviceManager.Close()
            End Using
        Catch ex As Vintasoft.Twain.TwainException
            Console.WriteLine("Error: " + ex.Message)
        End Try
    End Sub
    
    
    
    public static void AcquireImageModal()
    {
        try
        {
            // create the device manager
            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;
    
                device.ShowUI = false;
                device.DisableAfterAcquire = true;
    
                // open the device
                device.Open();
    
                // set acquisition parameters
                device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                device.PixelType = Vintasoft.Twain.PixelType.BW;
    
                // create directory for TIFF file
                string directoryForImages = System.IO.Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory());
                directoryForImages = System.IO.Path.Combine(directoryForImages, "Images");
                if (!System.IO.Directory.Exists(directoryForImages))
                    System.IO.Directory.CreateDirectory(directoryForImages);
    
                string multipageTiffFilename = System.IO.Path.Combine(directoryForImages, "multipage.tif");
    
                // acquire image(s) from the device
                Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
                int imageIndex = 0;
                do
                {
                    acquireModalState = device.AcquireModal();
                    switch (acquireModalState)
                    {
                        case Vintasoft.Twain.AcquireModalState.ImageAcquiring:
                            // output current state
                            System.Console.Write(string.Format("Image is acquiring ", imageIndex++));
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ImageAcquiringProgress:
                            // output current state
                            System.Console.Write(string.Format("{0}% ", device.ImageAcquiringProgressValue));
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                            // save the acquired image to a file
                            device.AcquiredImage.Save(multipageTiffFilename);
                            // dispose the acquired image
                            device.AcquiredImage.Dispose();
                            // output current state
                            System.Console.WriteLine(string.Format("Image{0} is saved.", imageIndex++));
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanCompleted:
                            // output current state
                            System.Console.WriteLine("Scan is completed.");
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanCanceled:
                            // output current state
                            System.Console.WriteLine("Scan is canceled.");
                            break;
    
                        case Vintasoft.Twain.AcquireModalState.ScanFailed:
                            // output current state
                            System.Console.WriteLine(string.Format("Scan is 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);
        }
    }
    
    

    Requirements

    Target Platforms: .NET 8, .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    See Also