VintaSoft Twain .NET SDK 14.0: Documentation for .NET developer
In This Topic
    How to process acquired image?
    In This Topic
    Here is an example that demonstrates how to acquire images from the device, process them and save to multipage TIFF file only not blank images:
    namespace TwainExamples_CSharp
    {
        public partial class How_to_process_acquired_image : System.Windows.Forms.Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            Vintasoft.Twain.DeviceManager _deviceManager;
    
    
    
            public How_to_process_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.ScanCanceled += new System.EventHandler(device_ScanCanceled);
                    device.ScanFailed += new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
                    device.ScanFinished += new System.EventHandler(device_ScanFinished);
    
                    // set scanning settings
                    device.TransferMode = Vintasoft.Twain.TransferMode.Memory;
                    device.ShowUI = false;
    
                    // 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)
            {
                Vintasoft.Twain.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, System.EventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Scan is canceled.");
            }
    
            /// <summary>
            /// Scan is failed.
            /// </summary>
            private void device_ScanFailed(object sender, Vintasoft.Twain.ScanFailedEventArgs e)
            {
                System.Windows.Forms.MessageBox.Show("Scan is failed: " + e.ErrorString);
            }
    
            /// <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.ScanCanceled -= new System.EventHandler(device_ScanCanceled);
                device.ScanFailed -= new System.EventHandler<Vintasoft.Twain.ScanFailedEventArgs>(device_ScanFailed);
                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.");
            }
    
    
            /// <summary>
            /// Process the acquired image.
            /// </summary>
            private void ProcessAcquiredImage(Vintasoft.Twain.AcquiredImage acquiredImage)
            {
                System.Console.WriteLine(string.Format("Image ({0})", acquiredImage.ImageInfo));
    
                try
                {
                    // subscribe to the processing events
                    acquiredImage.Processing += new System.EventHandler<Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingEventArgs>(acquiredImage_Processing);
                    acquiredImage.Progress += new System.EventHandler<Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
                    acquiredImage.Processed += new System.EventHandler<Vintasoft.Twain.ImageProcessing.AcquiredImageProcessedEventArgs>(acquiredImage_Processed);
    
                    // despeckle image
                    acquiredImage.Despeckle(8, 25, 30, 400);
                    // deskew image
                    acquiredImage.Deskew(Vintasoft.Twain.BorderColor.AutoDetect, 1, 1);
                    // detect border of image
                    acquiredImage.DetectBorder(5);
                }
                catch (Vintasoft.Twain.ImagingException ex)
                {
                    System.Console.WriteLine("Error: " + ex.Message);
                }
                finally
                {
                    // unsubscribe from the processing events
                    acquiredImage.Processing -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingEventArgs>(acquiredImage_Processing);
                    acquiredImage.Progress -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingProgressEventArgs>(acquiredImage_Progress);
                    acquiredImage.Processed -= new System.EventHandler<Vintasoft.Twain.ImageProcessing.AcquiredImageProcessedEventArgs>(acquiredImage_Processed);
                }
            }
    
            /// <summary>
            /// Image processing is started.
            /// </summary>
            private void acquiredImage_Processing(object sender, Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingEventArgs e)
            {
                System.Console.Write(e.Action.ToString() + " ");
            }
    
            /// <summary>
            /// Image processing is in progress.
            /// </summary>
            private void acquiredImage_Progress(object sender, Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingProgressEventArgs e)
            {
                System.Console.Write(".");
            }
    
            /// <summary>
            /// Image processing is finished.
            /// </summary>
            private void acquiredImage_Processed(object sender, Vintasoft.Twain.ImageProcessing.AcquiredImageProcessedEventArgs e)
            {
                System.Console.WriteLine(" finished");
            }
    
        }
    }
    
    
    Partial Public Class How_to_process_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.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
                AddHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
                AddHandler device1.ScanFinished, New EventHandler(AddressOf device_ScanFinished)
    
                ' set scanning settings
                device1.TransferMode = Vintasoft.Twain.TransferMode.Memory
                device1.ShowUI = False
    
                ' 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)
            Dim acquiredImage As Vintasoft.Twain.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 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.ScanCanceled, New EventHandler(AddressOf device_ScanCanceled)
            RemoveHandler device1.ScanFailed, New EventHandler(Of Vintasoft.Twain.ScanFailedEventArgs)(AddressOf device_ScanFailed)
            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
    
    
        ''' <summary>
        ''' Process the acquired image.
        ''' </summary>
        Private Sub ProcessAcquiredImage(ByVal acquiredImage As Vintasoft.Twain.AcquiredImage)
            Console.WriteLine(String.Format("Image ({0})", acquiredImage.ImageInfo))
    
            Try
                ' subscribe to the processing events
                AddHandler acquiredImage.Processing, New EventHandler(Of Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
                AddHandler acquiredImage.Progress, New EventHandler(Of Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
                AddHandler acquiredImage.Processed, New EventHandler(Of Vintasoft.Twain.ImageProcessing.AcquiredImageProcessedEventArgs)(AddressOf acquiredImage_Processed)
    
                ' despeckle image
                acquiredImage.Despeckle(8, 25, 30, 400)
                ' deskew image
                acquiredImage.Deskew(Vintasoft.Twain.BorderColor.AutoDetect, 5, 5)
                ' detect border of image
                acquiredImage.DetectBorder(5)
            Catch ex As Vintasoft.Twain.ImagingException
                Console.WriteLine("Error: " + ex.Message)
            Finally
                ' unsubscribe from the processing events
                RemoveHandler acquiredImage.Processing, New EventHandler(Of Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingEventArgs)(AddressOf acquiredImage_Processing)
                RemoveHandler acquiredImage.Progress, New EventHandler(Of Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingProgressEventArgs)(AddressOf acquiredImage_Progress)
                RemoveHandler acquiredImage.Processed, New EventHandler(Of Vintasoft.Twain.ImageProcessing.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 Vintasoft.Twain.ImageProcessing.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 Vintasoft.Twain.ImageProcessing.AcquiredImageProcessingProgressEventArgs)
            Console.Write(".")
        End Sub
    
        ''' <summary>
        ''' Image processing is finished.
        ''' </summary>
        Private Sub acquiredImage_Processed(ByVal sender As Object, ByVal e As Vintasoft.Twain.ImageProcessing.AcquiredImageProcessedEventArgs)
            Console.WriteLine(" finished")
        End Sub
    
    End Class