VintaSoft Twain .NET SDK 15.1: Documentation for .NET developer
In This Topic
    How to process an image, which is acquired from SANE device?
    In This Topic
    Here is an example that demonstrates how to acquire images from SANE device, process images and save only not blank images to a multipage TIFF file:
    /// <summary>
    /// Acquires images from SANE device and processes images.
    /// </summary>
    public void AcquireImagesFromSaneDeviceAndProcessImages()
    {
        // create SANE device manager
        using (Vintasoft.Sane.SaneLocalDeviceManager deviceManager = new Vintasoft.Sane.SaneLocalDeviceManager())
        {
            // open SANE device manager
            deviceManager.Open();
    
            // get count of SANE devices
            int deviceCount = deviceManager.Devices.Count;
            if (deviceCount == 0)
            {
                System.Console.WriteLine("Devices are not found.");
                return;
            }
    
            // select the first SANE device
            Vintasoft.Sane.SaneLocalDevice device = deviceManager.Devices[0];
    
            // open SANE device
            device.Open();
    
            Vintasoft.Sane.SaneAcquiredImage acquiredImage;
            do
            {
                try
                {
                    // acquire image from SANE device
                    acquiredImage = device.AcquireImageSync();
                    // if image is received
                    if (acquiredImage != null)
                    {
                        System.Console.WriteLine("Image is acquired.");
    
                        // process acquired image
                        ProcessSaneAcquiredImage(acquiredImage);
                    }
                    // if image is not received
                    else
                    {
                        System.Console.WriteLine("Scan is completed.");
                        break;
                    }
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(string.Format("Scan is failed: {0}", ex.Message));
                    break;
                }
            }
            // while device has more images to scan
            while (device.HasMoreImagesToScan);
    
            // close SANE device
            device.Close();
    
            // close SANE device manager
            deviceManager.Close();
        }
    
        System.Console.ReadLine();
    }
    
    /// <summary>
    /// Processes image, which is acquired from SANE device.
    /// </summary>
    private void ProcessSaneAcquiredImage(Vintasoft.Sane.SaneAcquiredImage acquiredImage)
    {
        try
        {
            // subscribe to the processing events
            acquiredImage.Processing += new System.EventHandler<Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingEventArgs>(acquiredImage_Processing);
            acquiredImage.Progress += new System.EventHandler<Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
            acquiredImage.Processed += new System.EventHandler<Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessedEventArgs>(acquiredImage_Processed);
    
            // despeckle image
            acquiredImage.Despeckle(8, 25, 30, 400);
            // deskew image
            acquiredImage.Deskew(Vintasoft.Sane.ImageProcessing.SaneBorderColor.AutoDetect, 1, 1);
            // detect border of image
            acquiredImage.DetectBorder(5);
        }
        catch (System.Exception ex)
        {
            System.Console.WriteLine("Error: " + ex.Message);
        }
        finally
        {
            // unsubscribe from the processing events
            acquiredImage.Processing -= new System.EventHandler<Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingEventArgs>(acquiredImage_Processing);
            acquiredImage.Progress -= new System.EventHandler<Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
            acquiredImage.Processed -= new System.EventHandler<Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessedEventArgs>(acquiredImage_Processed);
        }
    }
    
    /// <summary>
    /// Image processing is started.
    /// </summary>
    private void acquiredImage_Processing(object sender, Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingEventArgs e)
    {
        System.Console.Write(e.Action.ToString() + " ");
    }
    
    /// <summary>
    /// Image processing is in progress.
    /// </summary>
    private void acquiredImage_Progress(object sender, Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingProgressEventArgs e)
    {
        System.Console.Write(".");
    }
    
    /// <summary>
    /// Image processing is finished.
    /// </summary>
    private void acquiredImage_Processed(object sender, Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessedEventArgs e)
    {
        System.Console.WriteLine(" finished");
    }
    
    ''' <summary>
    ''' Acquires images from SANE device and processes images.
    ''' </summary>
    Public Sub AcquireImagesFromSaneDeviceAndProcessImages()
            ' create SANE device manager
            Using deviceManager As New Vintasoft.Sane.SaneLocalDeviceManager()
                    ' open SANE device manager
                    deviceManager.Open()
    
                    ' get count of SANE devices
                    Dim deviceCount As Integer = deviceManager.Devices.Count
                    If deviceCount = 0 Then
                            System.Console.WriteLine("Devices are not found.")
                            Return
                    End If
    
                    ' select the first SANE device
                    Dim device As Vintasoft.Sane.SaneLocalDevice = deviceManager.Devices(0)
    
                    ' open SANE device
                    device.Open()
    
                    Dim acquiredImage As Vintasoft.Sane.SaneAcquiredImage
                    Do
                            Try
                                    ' acquire image from SANE device
                                    acquiredImage = device.AcquireImageSync()
                                    ' if image is received
                                    If acquiredImage IsNot Nothing Then
                                            System.Console.WriteLine("Image is acquired.")
    
                                            ' process acquired image
                                            ProcessSaneAcquiredImage(acquiredImage)
                                    Else
                                            ' if image is not received
                                            System.Console.WriteLine("Scan is completed.")
                                            Exit Try
                                    End If
                            Catch ex As System.Exception
                                    System.Console.WriteLine(String.Format("Scan is failed: {0}", ex.Message))
                                    Exit Try
                            End Try
                    ' while device has more images to scan
                    Loop While device.HasMoreImagesToScan
    
                    ' close SANE device
                    device.Close()
    
                    ' close SANE device manager
                    deviceManager.Close()
            End Using
    
            System.Console.ReadLine()
    End Sub
    
    ''' <summary>
    ''' Processes image, which is acquired from SANE device.
    ''' </summary>
    Private Sub ProcessSaneAcquiredImage(acquiredImage As Vintasoft.Sane.SaneAcquiredImage)
            Try
                    ' subscribe to the processing events
                    AddHandler acquiredImage.Processing, New System.EventHandler(Of Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
                    AddHandler acquiredImage.Progress, New System.EventHandler(Of Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
                    AddHandler acquiredImage.Processed, New System.EventHandler(Of Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
    
                    ' despeckle image
                    acquiredImage.Despeckle(8, 25, 30, 400)
                    ' deskew image
                    acquiredImage.Deskew(Vintasoft.Sane.ImageProcessing.SaneBorderColor.AutoDetect, 1, 1)
                    ' detect border of image
                    acquiredImage.DetectBorder(5)
            Catch ex As System.Exception
                    System.Console.WriteLine("Error: " & ex.Message)
            Finally
                    ' unsubscribe from the processing events
                    RemoveHandler acquiredImage.Processing, New System.EventHandler(Of Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
                    RemoveHandler acquiredImage.Progress, New System.EventHandler(Of Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
                    RemoveHandler acquiredImage.Processed, New System.EventHandler(Of Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
            End Try
    End Sub
    
    ''' <summary>
    ''' Image processing is started.
    ''' </summary>
    Private Sub acquiredImage_Processing(sender As Object, e As Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingEventArgs)
            System.Console.Write(e.Action.ToString() & " ")
    End Sub
    
    ''' <summary>
    ''' Image processing is in progress.
    ''' </summary>
    Private Sub acquiredImage_Progress(sender As Object, e As Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessingProgressEventArgs)
            System.Console.Write(".")
    End Sub
    
    ''' <summary>
    ''' Image processing is finished.
    ''' </summary>
    Private Sub acquiredImage_Processed(sender As Object, e As Vintasoft.Sane.ImageProcessing.SaneAcquiredImageProcessedEventArgs)
            System.Console.WriteLine(" finished")
    End Sub