VintaSoft Twain .NET SDK v10.2
In This Topic
    How to process acquired image?
    In This Topic

    Example: Here is an example that shows how to acquire images from the device, process them and save to multipage TIFF file only not blank images.

    Imports System.Windows.Forms
    Imports Vintasoft.Twain
    Imports Vintasoft.Twain.ImageProcessing
    
    Partial Public Class How_to_process_acquired_image
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As DeviceManager
    
    
    
        Public Sub New()
            InitializeComponent()
    
            ' create and open device manager
            _deviceManager = New DeviceManager(Me)
            _deviceManager.Open()
        End Sub
    
    
    
        ''' <summary>
        ''' Acquire images asynchronously.
        ''' </summary>
        Public Sub AcquireImagesAsynchronously()
            Try
                ' get reference to the default device
                Dim device1 As Device = _deviceManager.DefaultDevice
    
                ' subscribe to the device events
                AddHandler device1.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
                AddHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
                AddHandler device1.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = TransferMode.Memory
                device1.ShowUI = False
    
                ' acquire images asynchronously
                device1.Acquire()
            Catch ex As 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)
            Dim acquiredImage As AcquiredImage = e.Image
    
            ' despeckle/deskew/detect border
            ProcessAcquiredImage(acquiredImage)
            ' add image to multipage TIFF file if image is not blank
            If Not acquiredImage.IsBlank(0.01F) Then
                acquiredImage.Save("d:\test.tif")
            End If
    
            ' dispose the acquired image
            acquiredImage.Dispose()
        End Sub
    
        ''' <summary>
        ''' Scan is canceled.
        ''' </summary>
        Private Sub device_ScanCanceled(ByVal sender As Object, ByVal e As EventArgs)
            MessageBox.Show("Scan is canceled.")
        End Sub
    
        ''' <summary>
        ''' Scan is failed.
        ''' </summary>
        Private Sub device_ScanFailed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ScanFailedEventArgs)
            MessageBox.Show("Scan is failed: " + Convert.ToString(e.ErrorString))
        End Sub
    
        ''' <summary>
        ''' Scan is finished.
        ''' </summary>
        Private Sub device_ScanFinished(ByVal sender As Object, ByVal e As EventArgs)
            Dim device1 As Device = DirectCast(sender, Device)
    
            ' unsubscribe from device events
            RemoveHandler device1.ImageAcquired, New EventHandler(Of ImageAcquiredEventArgs)(AddressOf device_ImageAcquired)
            RemoveHandler device1.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device1.ScanFailed, New EventHandler(Of ScanFailedEventArgs)(AddressOf device_ScanFailed)
            RemoveHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
            ' if device is not closed
            If device1.State <> DeviceState.Closed Then
                ' close the device
                device1.Close()
            End If
    
            MessageBox.Show("Scan is finished.")
        End Sub
    
    
        ''' <summary>
        ''' Process the acquired image.
        ''' </summary>
        Private Sub ProcessAcquiredImage(ByVal acquiredImage As AcquiredImage)
            Console.WriteLine(String.Format("Image ({0})", acquiredImage.ImageInfo))
    
            Try
                ' subscribe to the processing events
                AddHandler acquiredImage.Processing, New EventHandler(Of AcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
                AddHandler acquiredImage.Progress, New EventHandler(Of AcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
                AddHandler acquiredImage.Processed, New EventHandler(Of AcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
    
                ' despeckle image
                acquiredImage.Despeckle(8, 25, 30, 400)
                ' deskew image
                acquiredImage.Deskew(BorderColor.AutoDetect, 5, 5)
                ' detect border of image
                acquiredImage.DetectBorder(5)
            Catch ex As ImagingException
                Console.WriteLine("Error: " + ex.Message)
            Finally
                ' unsubscribe from the processing events
                RemoveHandler acquiredImage.Processing, New EventHandler(Of AcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
                RemoveHandler acquiredImage.Progress, New EventHandler(Of AcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
                RemoveHandler acquiredImage.Processed, New EventHandler(Of AcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
            End Try
        End Sub
    
        ''' <summary>
        ''' Image processing is started.
        ''' </summary>
        Private Sub acquiredImage_Processing(ByVal sender As Object, ByVal e As AcquiredImageProcessingEventArgs)
            Console.Write(e.Action.ToString() + " ")
        End Sub
    
        ''' <summary>
        ''' Image processing is in progress.
        ''' </summary>
        Private Sub acquiredImage_Progress(ByVal sender As Object, ByVal e As AcquiredImageProcessingProgressEventArgs)
            Console.Write(".")
        End Sub
    
        ''' <summary>
        ''' Image processing is finished.
        ''' </summary>
        Private Sub acquiredImage_Processed(ByVal sender As Object, ByVal e As AcquiredImageProcessedEventArgs)
            Console.WriteLine(" finished")
        End Sub
    
    End Class
                  
    
    using System;
    using System.Windows.Forms;
    using Vintasoft.Twain;
    using Vintasoft.Twain.ImageProcessing;
    
    namespace TwainExamples_CSharp
    {
        public partial class How_to_process_acquired_image : Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            DeviceManager _deviceManager;
    
    
    
            public How_to_process_acquired_image()
            {
                InitializeComponent();
    
                // create and open device manager
                _deviceManager = new DeviceManager(this);
                _deviceManager.Open();
            }
    
    
    
            /// <summary>
            /// Acquire images asynchronously.
            /// </summary>
            public void AcquireImagesAsynchronously()
            {
                try
                {
                    // get reference to the default device
                    Device device = _deviceManager.DefaultDevice;
    
                    // subscribe to the device events
                    device.ImageAcquired += new EventHandler(device_ImageAcquired);
                    device.ScanCanceled += new EventHandler(device_ScanCanceled);
                    device.ScanFailed += new EventHandler(device_ScanFailed);
                    device.ScanFinished += new EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = TransferMode.Memory;
                    device.ShowUI = false;
    
                    // acquire images asynchronously
                    device.Acquire();
                }
                catch (TwainException ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            /// <summary>
            /// Image is acquired.
            /// </summary>
            private void device_ImageAcquired(object sender, Vintasoft.Twain.ImageAcquiredEventArgs e)
            {
                AcquiredImage acquiredImage = e.Image;
    
                // despeckle/deskew/detect border
                ProcessAcquiredImage(acquiredImage);
                // add image to multipage TIFF file if image is not blank
                if (!acquiredImage.IsBlank(0.01f))
                    acquiredImage.Save(@"d:\test.tif");
    
                // dispose the acquired image
                acquiredImage.Dispose();
            }
    
            /// <summary>
            /// Scan is canceled.
            /// </summary>
            private void device_ScanCanceled(object sender, EventArgs e)
            {
                MessageBox.Show("Scan is canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
            {
                MessageBox.Show("Scan is failed: " + e.ErrorString);
            }
    
            /// <summary>
            /// Scan is finished.
            /// </summary>
            void device_ScanFinished(object sender, EventArgs e)
            {
                Device device = (Device)sender;
    
                // unsubscribe from device events
                device.ImageAcquired -= new EventHandler(device_ImageAcquired);
                device.ScanCanceled -= new EventHandler(device_ScanCanceled);
                device.ScanFailed -= new EventHandler(device_ScanFailed);
                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.");
            }
    
    
            /// <summary>
            /// Process the acquired image.
            /// </summary>
            private void ProcessAcquiredImage(AcquiredImage acquiredImage)
            {
                Console.WriteLine(string.Format("Image ({0})", acquiredImage.ImageInfo));
    
                try
                {
                    // subscribe to the processing events
                    acquiredImage.Processing += new EventHandler(acquiredImage_Processing);
                    acquiredImage.Progress += new EventHandler(acquiredImage_Progress);
                    acquiredImage.Processed += new EventHandler(acquiredImage_Processed);
    
                    // despeckle image
                    acquiredImage.Despeckle(8, 25, 30, 400);
                    // deskew image
                    acquiredImage.Deskew(BorderColor.AutoDetect, 5, 5);
                    // detect border of image
                    acquiredImage.DetectBorder(5);
                }
                catch (ImagingException ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                }
                finally
                {
                    // unsubscribe from the processing events
                    acquiredImage.Processing -= new EventHandler(acquiredImage_Processing);
                    acquiredImage.Progress -= new EventHandler(acquiredImage_Progress);
                    acquiredImage.Processed -= new EventHandler(acquiredImage_Processed);
                }
            }
    
            /// <summary>
            /// Image processing is started.
            /// </summary>
            private void acquiredImage_Processing(object sender, AcquiredImageProcessingEventArgs e)
            {
                Console.Write(e.Action.ToString() + " ");
            }
    
            /// <summary>
            /// Image processing is in progress.
            /// </summary>
            private void acquiredImage_Progress(object sender, AcquiredImageProcessingProgressEventArgs e)
            {
                Console.Write(".");
            }
    
            /// <summary>
            /// Image processing is finished.
            /// </summary>
            private void acquiredImage_Processed(object sender, AcquiredImageProcessedEventArgs e)
            {
                Console.WriteLine(" finished");
            }
    
        }
    }