Get a Fast Twain Scan from a Scanner

Take the following steps to start a project and to add some code to find the fastest configuration for your scanner using the LEADTOOLS Fast TWAIN feature:

  1. Start Visual Studio .NET.

  2. Choose File->New->Project… from the menu.

  3. In the New Project dialog box, choose either "Visual C# Projects" or "VB Projects" in the Projects Type List, and choose "Windows Application" in the Templates List.

  4. Type the project name as "GetFastTwainScan" in the Project Name field, and then choose OK. If desired, type a new location for your project or select a directory using the Browse button, and then choose OK.

  5. In the "Solution Explorer" window, right-click on the "References" folder, and select "Add Reference…" from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and browse to Leadtools For .NET "<LEADTOOLS_INSTALLDIR>\Bin\Dotnet\Win32" folder and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Twain.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll

    Click the Select button and then press the OK button to add the above DLLs to the application.

  6. Go to the toolbox (View->Toolbox) and Drag and drop 3 instances of Button control to the top of the form and set the following properties for them:
Text Name
Select Source buttonSelectSource
Find Fast Config buttonFindFastConfig
Acquire Fast Config buttonAcquireFastConfig
  1. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code ) and add the following lines at the beginning of the file:

    C#
    VB
    Imports Leadtools 
    Imports Leadtools.Twain 
    Imports Leadtools.Codecs 
    Imports Leadtools.WinForms 
    using Leadtools; 
    using Leadtools.Twain; 
    using Leadtools.Codecs; 
    using Leadtools.WinForms; 

  2. Declare the following private variable:

    C#
    VB
    Private WithEvents twnSession As TwainSession 
    Private fastConfigResult As TwainFindFastConfigurationResult 
    private TwainSession twnSession; 
    private TwainFindFastConfigurationResult fastConfigResult; 

  3. Add an event handler to the Form1 Load event and code it as follows:

    C#
    VB
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       Try 
          ' Unlock Document support. 
          ' Note that this is a sample key, which will not work in your toolkit. 
          Dim MY_LICENSE_FILE As String = "d:\temp\TestLic.lic" 
          Dim MY_DEVELOPER_KEY As String = "xyz123abc" 
          RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY) 
          twnSession = new TwainSession 
          twnSession.Startup(Me.Handle, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    private void Form1_Load(object sender, System.EventArgs e) 
    { 
       try 
       { 
          // Unlock Document support. 
          // Note that this is a sample key, which will not work in your toolkit. 
          string MY_LICENSE_FILE = "d:\\temp\\TestLic.lic"; 
          string MY_DEVELOPER_KEY = "xyz123abc"; 
          RasterSupport.SetLicense(MY_LICENSE_FILE, MY_DEVELOPER_KEY); 
          twnSession = new TwainSession(); 
          twnSession.Startup(this.Handle, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  4. Add an event handler to the Form1 Closing event and code it as follows:

    C#
    VB
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 
       Try 
          twnSession.Shutdown() 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
       try 
       { 
          twnSession.Shutdown(); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  5. Add an event handler to the buttonSelectSource Click event and code it as follows:

    C#
    VB
    Private Sub buttonSelectSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSelectSource.Click 
       Try 
          twnSession.SelectSource(String.Empty) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    private void buttonSelectSource_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
         twnSession.SelectSource(string.Empty); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  6. Add an event handler to the buttonFindFastConfig Click event and code it as follows:

    C#
    VB
    Private Sub buttonFindFastConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonFindFastConfig.Click 
       Try 
          twnSession.EnableFastConfigurationEvent = False 
          fastConfigResult = twnSession.FindFastConfiguration("C:\Users\Public\Documents\LEADTOOLS Images", TwainFastUserInterfaceFlags.Show Or TwainFastUserInterfaceFlags.UseThread, 8, 5, Nothing) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    private void buttonFindFastConfig_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          twnSession.EnableFastConfigurationEvent = false; 
          fastConfigResult = twnSession.FindFastConfiguration(@"C:\Users\Public\Documents\LEADTOOLS Images", TwainFastUserInterfaceFlags.Show |   TwainFastUserInterfaceFlags.UseThread, 8, 5, null); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  7. Add an event handler to the buttonAcquireFastConfig Click event and code it as follows:

    C#
    VB
    Private Sub buttonAcquireFastConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAcquireFastConfig.Click 
       Try 
          twnSession.EnableAcquireMultiPageEvent = False 
          twnSession.AcquireFast("C:\Users\Public\Documents\LEADTOOLS Images\test", _ 
                                 TwainFastUserInterfaceFlags.Show, _ 
                                 fastConfigResult.Best.TransferMechanism, _ 
                                 fastConfigResult.Best.ImageFormat, _ 
                                 fastConfigResult.Best.BitsPerPixel, _ 
                                 True, _ 
                                 fastConfigResult.Best.BufferSize, _ 
                                 True) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    private void buttonAcquireFastConfig_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          twnSession.EnableAcquireMultiPageEvent = false; 
          twnSession.AcquireFast(@"C:\Users\Public\Documents\LEADTOOLS Images\test", 
               TwainFastUserInterfaceFlags.Show, 
               fastConfigResult.Best.TransferMechanism, 
               fastConfigResult.Best.ImageFormat, 
               fastConfigResult.Best.BitsPerPixel, 
               true, 
               fastConfigResult.Best.BufferSize, 
               true); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  8. Build, and Run the program to test it.
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize