VintaSoft Twain .NET SDK 12.1
In This Topic
    How to get the extended information about image acquired from TWAIN scanner?
    In This Topic
    VintaSoft Twain .NET SDK allows to retrieve the extended
    information about acquired image. The extended image information can contain information about document/page number, image frame, barcodes or patch codes found on a page, information about despeckle, deskew process and more.

    You should proceed with the following steps if you want to retrieve the extended image information:
    1. Check that the retrieving of the extended image information is supported by the device.
    2. Choose which information you want to retrieve.
    3. Retrieve the extended image information about acquired image right after the image is acquired.

    Here is an example that demonstrates how to retrieve the extended information (document number, page number, camera, frame number, frame, pixel flavor) about acquired image:
    namespace TwainExamples_CSharp
    {
        public partial class How_to_get_extended_information_about_acquired_image : System.Windows.Forms.Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            Vintasoft.Twain.DeviceManager _deviceManager;
    
    
    
            public How_to_get_extended_information_about_acquired_image()
            {
                InitializeComponent();
    
                // create and open device manager
                _deviceManager = new Vintasoft.Twain.DeviceManager(this);
                _deviceManager.Open();
            }
    
    
    
            /// <summary>
            /// Acquire images asynchronously.
            /// </summary>
            public void AcquireImagesAsynchronously()
            {
                try
                {
                    // get reference to the default device
                    Vintasoft.Twain.Device device = _deviceManager.DefaultDevice;
    
                    // subscribe to the device events
                    device.ImageAcquired += new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
                    device.ScanFinished += new System.EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                    device.ShowUI = false;
                    device.DisableAfterAcquire = true;
    
                    // open the device
                    device.Open();
    
                    // specify that extended information about acquired image is necessary
                    device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.DocumentNumber));
                    device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PageNumber));
                    device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Camera));
                    device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.FrameNumber));
                    device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Frame));
                    device.ExtendedImageInfo.Add(new Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PixelFlavor));
    
                    // acquire images asynchronously
                    device.Acquire();
                }
                catch (Vintasoft.Twain.TwainException ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
            }
    
            /// <summary>
            /// Image is acquired.
            /// </summary>
            private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
            {
                // output an extended information about the acquired image
    
                extendedImageInfoAboutAcquiredImageTextBox.Text += "IMAGE IS ACQUIRED" + System.Environment.NewLine;
                extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
    
                Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
                for (int i = 0; i < device.ExtendedImageInfo.Count; i++)
                {
                    AddExtendedImageInfoToResultTextBox(i, device.ExtendedImageInfo[i]);
                }
                extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
    
                // dispose the acquired image
                e.Image.Dispose();            
            }
    
            /// <summary>
            /// Add an extended image info to the result text box.
            /// </summary>
            private void AddExtendedImageInfoToResultTextBox(int index, Vintasoft.Twain.ExtendedImageInfo info)
            {
                if (!info.IsValueValid)
                    return;
    
                extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("Extended image info {0}", index);
                extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
    
                extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("  Name={0}", System.Enum.GetName(typeof(Vintasoft.Twain.ExtendedImageInfoId), info.InfoId));
                extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
    
                extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("  Id={0}", info.InfoId);
                extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
    
                extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("  Value type={0}", info.ValueType);
                extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
    
                Vintasoft.Twain.TwainOneValueContainer oneDeviceCapabilityValue = info.Value as Vintasoft.Twain.TwainOneValueContainer;
                if (oneDeviceCapabilityValue != null)
                {
                    extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("  Value={0}", oneDeviceCapabilityValue.Value);
                    extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
                }
                else
                {
                    Vintasoft.Twain.TwainArrayValueContainer arrayDeviceCapabilityValue = info.Value as Vintasoft.Twain.TwainArrayValueContainer;
                    if (arrayDeviceCapabilityValue != null)
                    {
                        extendedImageInfoAboutAcquiredImageTextBox.Text += "Values: ";
                        if (arrayDeviceCapabilityValue.Values.GetType() == typeof(byte[]))
                        {
                            extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("byte[{0}]", arrayDeviceCapabilityValue.Values.Length);
                        }
                        else
                        {
                            for (int i = 0; i < arrayDeviceCapabilityValue.Values.Length; i++)
                                extendedImageInfoAboutAcquiredImageTextBox.Text += string.Format("{0}, ", arrayDeviceCapabilityValue.Values.GetValue(i));
                        }
                        extendedImageInfoAboutAcquiredImageTextBox.Text += System.Environment.NewLine;
                    }
                }
            }
            
            /// <summary>
            /// Scan is finished.
            /// </summary>
            void device_ScanFinished(object sender, System.EventArgs e)
            {
                Vintasoft.Twain.Device device = (Vintasoft.Twain.Device)sender;
    
                // unsubscribe from device events
                device.ImageAcquired -= new System.EventHandler<Vintasoft.Twain.ImageAcquiredEventArgs>(device_ImageAcquired);
                device.ScanFinished -= new System.EventHandler(device_ScanFinished);
    
                // if device is not closed
                if (device.State != Vintasoft.Twain.DeviceState.Closed)
                    // close the device
                    device.Close();
    
                System.Windows.Forms.MessageBox.Show("Scan is finished.");
            }
    
        }
    }
    
    
    Partial Public Class How_to_get_extended_information_about_acquired_image
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As Vintasoft.Twain.DeviceManager
    
    
    
        Public Sub New()
            InitializeComponent()
    
            ' create and open device manager
            _deviceManager = New Vintasoft.Twain.DeviceManager(Me)
            _deviceManager.Open()
        End Sub
    
    
    
        ''' <summary>
        ''' Acquire images asynchronously.
        ''' </summary>
        Public Sub AcquireImagesAsynchronously()
            Try
                ' get reference to the default device
                Dim device1 As Vintasoft.Twain.Device = _deviceManager.DefaultDevice
    
                ' subscribe to the device events
                AddHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device1.ShowUI = False
                device1.DisableAfterAcquire = True
    
                ' open the device
                device1.Open()
    
                ' specify that extended information about acquired image is necessary
                device1.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.DocumentNumber))
                device1.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PageNumber))
                device1.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Camera))
                device1.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.FrameNumber))
                device1.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.Frame))
                device1.ExtendedImageInfo.Add(New Vintasoft.Twain.ExtendedImageInfo(Vintasoft.Twain.ExtendedImageInfoId.PixelFlavor))
    
                ' acquire images asynchronously
                device1.Acquire()
            Catch ex As Vintasoft.Twain.TwainException
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
        ''' <summary>
        ''' Image is acquired.
        ''' </summary>
        Private Sub device_ImageAcquired(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageAcquiredEventArgs)
            ' output an extended information about the acquired image
    
            extendedImageInfoAboutAcquiredImageTextBox.Text += "IMAGE IS ACQUIRED" + Environment.NewLine
            extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
    
            Dim device As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
            For i As Integer = 0 To device.ExtendedImageInfo.Count - 1
                AddExtendedImageInfoToResultTextBox(i, device.ExtendedImageInfo(i))
            Next
            extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
    
            ' dispose the acquired image
            e.Image.Dispose()
        End Sub
    
        ''' <summary>
        ''' Add an extended image info to the result text box.
        ''' </summary>
        Private Sub AddExtendedImageInfoToResultTextBox(ByVal index As Integer, ByVal info As Vintasoft.Twain.ExtendedImageInfo)
            If Not info.IsValueValid Then
                Return
            End If
    
            extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("Extended image info {0}", index)
            extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
    
            extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("  Name={0}", [Enum].GetName(GetType(Vintasoft.Twain.ExtendedImageInfoId), info.InfoId))
            extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
    
            extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("  Id={0}", info.InfoId)
            extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
    
            extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("  Value type={0}", info.ValueType)
            extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
    
            Dim oneDeviceCapabilityValue As Vintasoft.Twain.TwainOneValueContainer = TryCast(info.Value, Vintasoft.Twain.TwainOneValueContainer)
            If oneDeviceCapabilityValue IsNot Nothing Then
                extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("  Value={0}", oneDeviceCapabilityValue.Value)
                extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
            Else
                Dim arrayDeviceCapabilityValue As Vintasoft.Twain.TwainArrayValueContainer = TryCast(info.Value, Vintasoft.Twain.TwainArrayValueContainer)
                If arrayDeviceCapabilityValue IsNot Nothing Then
                    extendedImageInfoAboutAcquiredImageTextBox.Text += "Values: "
                    If arrayDeviceCapabilityValue.Values.[GetType]() Is GetType(Byte()) Then
                        extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("byte[{0}]", arrayDeviceCapabilityValue.Values.Length)
                    Else
                        For i As Integer = 0 To arrayDeviceCapabilityValue.Values.Length - 1
                            extendedImageInfoAboutAcquiredImageTextBox.Text += String.Format("{0}, ", arrayDeviceCapabilityValue.Values.GetValue(i))
                        Next
                    End If
                    extendedImageInfoAboutAcquiredImageTextBox.Text += Environment.NewLine
                End If
            End If
        End Sub
    
        ''' <summary>
        ''' Scan is finished.
        ''' </summary>
        Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            Dim device1 As Vintasoft.Twain.Device = DirectCast(sender, Vintasoft.Twain.Device)
    
            ' unsubscribe from device events
            RemoveHandler device1.ImageAcquired, New EventHandler(Of Vintasoft.Twain.ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
            ' if device is not closed
            If device1.State <> Vintasoft.Twain.DeviceState.Closed Then
                ' close the device
                device1.Close()
            End If
    
            MessageBox.Show("Scan is finished.")
        End Sub
    
    End Class
    
    


    Here is an example that demonstrates how to get information about barcodes recognized on acquired image:
    public void GetExtendedImageInfo()
    {
        using (Vintasoft.Twain.DeviceManager deviceManager = new Vintasoft.Twain.DeviceManager())
        {
            try
            {
                // open the device manager
                deviceManager.Open();
    
                deviceManager.ShowDefaultDeviceSelectionDialog();
    
                // get reference to the default device
                Vintasoft.Twain.Device device = deviceManager.DefaultDevice;
    
                // specify that device UI must not be used
                device.ShowUI = false;
                // specify that device must be closed after scan
                device.DisableAfterAcquire = true;
    
                // open the device
                device.Open();
    
                // specify that device should recognize barcodes in acquired images
                device.BarcodeRecognizer.IsEnabled = true;
    
                // acquire images from device
                Vintasoft.Twain.AcquireModalState acquireModalState = Vintasoft.Twain.AcquireModalState.None;
                do
                {
                    acquireModalState = device.AcquireModal();
    
                    switch (acquireModalState)
                    {
                        case Vintasoft.Twain.AcquireModalState.ImageAcquired:
                            // get information about recognized barcodes
                            Vintasoft.Twain.TwainBarcodeInfo[] recognizedBarcodes = device.BarcodeRecognizer.GetRecognitionResult();
                            // if barcodes are NOT found
                            if (recognizedBarcodes.Length == 0)
                            {
                                System.Console.WriteLine("Barcodes are NOT found.");
                            }
                            // if barcodes are found
                            else
                            {
                                System.Console.WriteLine(string.Format("{0} barcodes are found.", recognizedBarcodes.Length));
                                // for each recognized barcode
                                for (int i = 0; i < recognizedBarcodes.Length; i++)
                                {
                                    // output barcode type
                                    System.Console.Write(string.Format("BarcodeType={0} ", recognizedBarcodes[i].Type));
                                    // output barcode text
                                    System.Console.WriteLine(string.Format("BarcodeText='{0}'", recognizedBarcodes[i].Value));
                                }
                            }
    
                            // if acquired image is present (Native or Memory transfer mode is used)
                            if (device.AcquiredImage != null)
                                // dispose the acquired image
                                device.AcquiredImage.Dispose();
    
                            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);
                System.Console.ReadLine();
            }
        }
    }
    
    Public Sub GetExtendedImageInfo()
        Using deviceManager As New Vintasoft.Twain.DeviceManager()
            Try
                ' open the device manager
                deviceManager.Open()
    
                deviceManager.ShowDefaultDeviceSelectionDialog()
    
                ' get reference to the default device
                Dim device As Vintasoft.Twain.Device = deviceManager.DefaultDevice
    
                ' specify that device UI must not be used
                device.ShowUI = False
                ' specify that device must be closed after scan
                device.DisableAfterAcquire = True
    
                ' open the device
                device.Open()
    
                ' specify that device should recognize barcodes in acquired images
                device.BarcodeRecognizer.IsEnabled = True
    
                ' acquire images from device
                Dim acquireModalState__1 As Vintasoft.Twain.AcquireModalState = Vintasoft.Twain.AcquireModalState.None
                Do
                    acquireModalState__1 = device.AcquireModal()
    
                    Select Case acquireModalState__1
                        Case Vintasoft.Twain.AcquireModalState.ImageAcquired
                            ' get information about recognized barcodes
                            Dim recognizedBarcodes() As Vintasoft.Twain.TwainBarcodeInfo = device.BarcodeRecognizer.GetRecognitionResult()
                            ' if barcodes are Not found
                            If recognizedBarcodes.Length = 0 Then
                                Console.WriteLine("Barcodes are NOT found.")
                                ' if barcodes are found
                            Else
                                Console.WriteLine(String.Format("{0} barcodes are found.", recognizedBarcodes.Length))
                                ' for each recognized barcode
                                For i As Integer = 0 To recognizedBarcodes.Length - 1
                                    ' output barcode type
                                    Console.Write(String.Format("BarcodeType={0} ", recognizedBarcodes(i).Type))
                                    ' output barcode text
                                    Console.WriteLine(String.Format("BarcodeText='{0}'", recognizedBarcodes(i).Value))
                                Next i
                            End If
    
                            ' if acquired image is present (Native or Memory transfer mode is used)
                            If device.AcquiredImage IsNot Nothing Then
                                ' dispose the acquired image
                                device.AcquiredImage.Dispose()
                            End If
    
                            Exit Select
                    End Select
                Loop While acquireModalState__1 <> Vintasoft.Twain.AcquireModalState.None
    
                ' close the device
                device.Close()
    
                ' close the device manager
                deviceManager.Close()
            Catch ex As Vintasoft.Twain.TwainException
                Console.WriteLine("Error: " + ex.Message)
                Console.ReadLine()
            End Try
        End Using
    End Sub