Using the Fast Twain Feature

Take the following steps to start a project and to add some code that acquires the images using Fast Twain:

  1. Start Visual Studio.

  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 "UsingFastTwainFeature" 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 2 instances of RadioButton control to the top of the form and set the following properties for them:

    Text Name Checked
    Native radioNative False
    Memory radioMemory False
  7. Go to the toolbox (View->Toolbox) and Drag and drop a 1 instance of Button control to the top of the form and set the following properties for them:

    Text Name
    Fast Acquire buttonFastAcquire
  8. Go to the toolbox (View->Toolbox) and Drag and drop an instance of Check Box control to the top of the form and set the following properties for them:

    Name Name
    Use Buffer Size checkBoxUseBufferSize
  9. Go to the toolbox (View->Toolbox) and Drag and drop 2 instances of Text Box control to the top of the form and change the following properties:

    Property Value
    Name textBufferSize
    Name textFileName
  10. 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:

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

  11. Declare the following private variable:

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

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

    VB
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       textFileName.Text = "c:\twain_test.tif" 
       textBufferSize.Text = "2048" 
       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.InitializeMultithreaded) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void Form1_Load(object sender, System.EventArgs e) 
    { 
       textFileName.Text = @"c:\twain_test.tif"; 
       textBufferSize.Text = "2048"; 
       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.InitializeMultithreaded); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

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

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

  14. Add an event handler to the buttonFastAcquire Click event and code it as follows:

    VB
    Private Sub buttonFastAcquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonFastAcquire.Click 
       Dim transferMode As TwainTransferMode 
       Dim transferFormat As RasterImageFormat 
       If radioMemory.Checked Then 
          transferMode = TwainTransferMode.Buffer 
          transferFormat = RasterImageFormat.CcittGroup4 
       Else 
          transferMode = TwainTransferMode.Native 
          transferFormat = RasterImageFormat.Tif 
       End If 
       Try 
          twnSession.AcquireFast(textFileName.Text, _ 
                                 TwainFastUserInterfaceFlags.Show Or TwainFastUserInterfaceFlags.UseThread, _ 
                                 transferMode, _ 
                                 transferFormat, _ 
                                 1, _ 
                                 True, _ 
                                 Convert.ToInt32(textBufferSize.Text), _ 
                                 checkBoxUseBufferSize.Checked) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void buttonFastAcquire_Click(object sender, System.EventArgs e) 
    { 
       TwainTransferMode transferMode; 
       RasterImageFormat transferFormat; 
       if (radioMemory.Checked) 
       { 
          transferMode = TwainTransferMode.Buffer; 
          transferFormat = RasterImageFormat.CcittGroup4; 
       } 
       else 
       { 
          transferMode = TwainTransferMode.Native; 
          transferFormat = RasterImageFormat.Tif; 
       } 
       try 
       { 
          twnSession.AcquireFast(textFileName.Text, 
                                 TwainFastUserInterfaceFlags.Show |   TwainFastUserInterfaceFlags.UseThread, 
                                 transferMode, 
                                 transferFormat, 
                                 1, 
                                 true, 
                                 Convert.ToInt32(textBufferSize.Text), 
                                 checkBoxUseBufferSize.Checked); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(ex.Message); 
       } 
    } 

  15. Build, and Run the program to test it.
Help Version 20.0.2020.4.3
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS Imaging, Medical, and Document