VintaSoft Twain .NET SDK v10.2
In This Topic
    How to save acquired images in the database?
    In This Topic

    Example: Here is an example that shows how to save acquired images as multipage PDF document in memory. Later, PDF document can be saved in the database or transferred onto the server.

    Imports System.IO
    Imports System.Windows.Forms
    Imports Vintasoft.Twain
    Imports Vintasoft.Twain.ImageEncoders
    
    ''' <summary>
    ''' Class allows to scan and save image as PDF document to a database.
    ''' </summary>
    Partial Public Class How_to_save_acquired_images_to_database_as_PDF_document
        Inherits Form
    
        ''' <summary>
        ''' TWAIN device manager.
        ''' </summary>
        Private _deviceManager As DeviceManager
    
        ''' <summary>
        ''' Memory stream for PDF document.
        ''' </summary>
        Private _memoryStream As MemoryStream
    
    
    
        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.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
                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)
    
                ' disable UI
                device1.ShowUI = False
                ' set Memory (or Native) transfer mode
                device1.TransferMode = TransferMode.Memory
    
                ' 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)
            ' if memory stream is empty
            If _memoryStream Is Nothing Then
                ' get the first image as PDF stored in the memory
                _memoryStream = e.Image.GetAsStream(New TwainPdfEncoderSettings())
            Else
                ' add image to PDF stored in the memory
                e.Image.Save(_memoryStream, New TwainPdfEncoderSettings())
            End If
    
            ' dispose the acquired image
            e.Image.Dispose()
        End Sub
    
        ''' <summary>
        ''' Scan is completed.
        ''' </summary>
        Private Sub device_ScanCompleted(ByVal sender As Object, ByVal e As EventArgs)
            ' save memory stream to a database
        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.ScanCompleted, New EventHandler(AddressOf device_ScanCompleted)
            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
    
    End Class
                  
    
    using System;
    using System.IO;
    using System.Windows.Forms;
    using Vintasoft.Twain;
    using Vintasoft.Twain.ImageEncoders;
    
    namespace TwainExamples_CSharp
    {
        /// <summary>
        /// Class allows to scan and save image as PDF document to a database.
        /// </summary>
        public partial class How_to_save_acquired_images_to_database_as_PDF_document : Form
        {
    
            /// <summary>
            /// TWAIN device manager.
            /// </summary>
            DeviceManager _deviceManager;
    
            /// <summary>
            /// Memory stream for PDF document.
            /// </summary>
            MemoryStream _memoryStream;
    
    
    
            public How_to_save_acquired_images_to_database_as_PDF_document()
            {
                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.ScanCompleted += new EventHandler(device_ScanCompleted);
                    device.ScanCanceled += new EventHandler(device_ScanCanceled);
                    device.ScanFailed += new EventHandler(device_ScanFailed);
                    device.ScanFinished -= new EventHandler(device_ScanFinished);
    
                    // disable UI
                    device.ShowUI = false;
                    // set Memory (or Native) transfer mode
                    device.TransferMode = TransferMode.Memory;
    
                    // 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)
            {
                // if memory stream is empty
                if (_memoryStream == null)
                    // get the first image as PDF stored in the memory
                    _memoryStream = e.Image.GetAsStream(new TwainPdfEncoderSettings());
                else
                    // add image to PDF stored in the memory
                    e.Image.Save(_memoryStream, new TwainPdfEncoderSettings());
    
                // dispose the acquired image
                e.Image.Dispose();
            }
    
            /// <summary>
            /// Scan is completed.
            /// </summary>
            private void device_ScanCompleted(object sender, EventArgs e)
            {
                // save memory stream to a database
            }
    
            /// <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.ScanCompleted -= new EventHandler(device_ScanCompleted);
                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.");
            }
    
        }
    }