Acquire an Image from TWAIN Source

Take the following steps to create and run a program that acquires an image from a TWAIN source.

  1. Start Visual Studio.
  2. Choose File -> New -> Project… from the menu.
  3. In the New Project dialog box, choose "Windows Classic Desktop" from either the "Visual C#" or "Visual Basic" node in the Templates List. Then choose Windows Forms App (.NET Framework).
  4. Type the project name as "Acquiring an Image" in the project Name field. If desired, type a new location for your project or select a directory using the Browse button. Click 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, browse to the LEADTOOLS bin folder (<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32) and select the following DLLs:

    • Leadtools.dll
    • Leadtools.Twain.dll
    • Leadtools.Codecs.dll
    • Leadtools.Controls.WinForms.dll
      Click the Select button and then click the OK button to add the above DLLs to the application.
  6. Make sure Form1 is in design view. Go to the toolbox (View -> Toolbox) and drag and drop an instance of ImageViewer to the form. If you do not have ImageViewer in your toolbox, select Tools -> Choose Toolbox Items from the menu. Click Browse and then select Leadtools.Controls.WinForms.DLL from "<LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32" and then click Open and then click OK.

  7. Go to the toolbox (View -> Toolbox) and drag and drop 3 instances of the RadioButton control to the top of the form and set the following properties for them:
    Text Name Checked
    Native radioNative False
    Memory radioMemory False
    File radioFile False
  8. Go to the toolbox (View->Toolbox) and drag and drop 4 instances of the Button control to the top of the form and set the following properties for them:

    Text Name
    Acquire buttonAcquire
    Select Source buttonSelectSource
    Save Template File buttonSaveTemplateFile
    Load Template File buttonLoadTemplateFile
  9. Switch to the code view of Form1 (in the Solution Explorer, right-click Form1 then select View Code) and add the following lines at the beginning of the file:
    VB
    Imports Leadtools 
    Imports Leadtools.Twain 
    C#
    using Leadtools; 
    using Leadtools.Twain; 
  10. Declare the following private variable:
    VB
    Private WithEvents _twnSession As TwainSession 
    C#
    private TwainSession _twnSession; 
  11. Add an event handler to the Form1 Load event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code the Form1 Load event as follows:
    VB
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
       Try 
       	  Dim myLicenseFile As String = "C:\LEADTOOLS 20\Support\Common\License\leadtools.lic" 
          Dim myDeveloperKey As String = System.IO.File.ReadAllText("C:\LEADTOOLS 20\Support\Common\License\leadtools.lic.key") 
          RasterSupport.SetLicense(myLicenseFile, myDeveloperKey) 
          _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 
    C#
    private void Form1_Load(object sender, System.EventArgs e) 
    { 
       try 
       { 
    	  const string myLicenseFile = @"C:\LEADTOOLS 20\Support\Common\License\leadtools.lic"; 
          string myDeveloperKey = System.IO.File.ReadAllText(@"C:\LEADTOOLS 20\Support\Common\License\leadtools.lic.key"); 
          RasterSupport.SetLicense(myLicenseFile, myDeveloperKey); 
          _twnSession = new TwainSession(); 
          _twnSession.Startup(this.Handle, "manufacturer", "productFamily", "version", "application", TwainStartupFlags.None); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  12. Add an event handler to the Form1 Closing event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    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    
    C#
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
       try 
       { 
          _twnSession.Shutdown(); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  13. Add an event handler to the _twnSession AcquirePage event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub _twnSession_AcquirePage(ByVal sender As Object, ByVal e As TwainAcquirePageEventArgs) Handles _twnSession.AcquirePage 
       RasterImageViewer1.Image = e.Image 
    End Sub 
    C#
    private void _twnSession_AcquirePage(object sender, TwainAcquirePageEventArgs e) 
    { 
       rasterImageViewer1.Image = e.Image; 
    } 
  14. Add an event handler to the buttonAcquire Click event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub buttonAcquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonAcquire.Click 
       Try 
          _twnSession.Acquire(TwainUserInterfaceFlags.Show) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
    C#
    private void buttonAcquire_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          _twnSession.AcquirePage += new EventHandler<TwainAcquirePageEventArgs>(_twnSession_AcquirePage); 
          _twnSession.Acquire(TwainUserInterfaceFlags.Show); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  15. Add an event handler to the buttonSelectSource Click event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    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    
    C#
    private void buttonSelectSource_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          _twnSession.SelectSource(string.Empty); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  16. Add an event handler to the buttonSaveTemplateFile Click event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub buttonSaveTemplateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonSaveTemplateFile.Click 
       Try 
          _twnSession.SaveTemplateFile("c:\test.ltt") 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void buttonSaveTemplateFile_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          _twnSession.SaveTemplateFile(@"c:\test.ltt"); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  17. Add an event handler to the buttonLoadTemplateFile Click event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub buttonLoadTemplateFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonLoadTemplateFile.Click 
       Try 
          _twnSession.LoadTemplateFile("c:\test.ltt") 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
    C#
    private void buttonLoadTemplateFile_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          _twnSession.LoadTemplateFile(@"c:\test.ltt"); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  18. Add an event handler to the radioNative CheckedChanged event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub radioNative_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioNative.CheckedChanged 
       Try 
          Dim capability As New TwainCapability 
          capability.Information.ContainerType = TwainContainerType.OneValue 
          capability.Information.Type = TwainCapabilityType.ImageTransferMechanism 
          capability.OneValueCapability.ItemType = TwainItemType.Uint16 
          capability.OneValueCapability.Value = CUShort(TwainTransferMechanism.Native) 
          _twnSession.SetCapability(capability, TwainSetCapabilityMode.Set) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void radioNative_CheckedChanged(object sender, System.EventArgs e) 
    { 
       try 
       { 
          TwainCapability capability = new TwainCapability(); 
          capability.Information.ContainerType = TwainContainerType.OneValue; 
          capability.Information.Type = TwainCapabilityType.ImageTransferMechanism; 
          capability.OneValueCapability.ItemType = TwainItemType.Uint16; 
          capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.Native; 
          _twnSession.SetCapability(capability, TwainSetCapabilityMode.Set); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  19. Add an event handler to the radioMemory CheckedChanged event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub radioMemory_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioMemory.CheckedChanged 
       Try 
          Dim capability As New TwainCapability 
          capability.Information.ContainerType = TwainContainerType.OneValue 
          capability.Information.Type = TwainCapabilityType.ImageTransferMechanism 
                           
          capability.OneValueCapability.ItemType = TwainItemType.Uint16 
          capability.OneValueCapability.Value = CUShort(TwainTransferMechanism.Memory) 
                           
          _twnSession.SetCapability(capability, TwainSetCapabilityMode.Set) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void radioMemory_CheckedChanged(object sender, System.EventArgs e) 
    { 
       try 
       { 
          TwainCapability capability = new TwainCapability(); 
          capability.Information.ContainerType = TwainContainerType.OneValue; 
          capability.Information.Type = TwainCapabilityType.ImageTransferMechanism; 
                           
          capability.OneValueCapability.ItemType = TwainItemType.Uint16; 
          capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.Memory; 
                           
          _twnSession.SetCapability(capability, TwainSetCapabilityMode.Set); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  20. Add an event handler to the radioFile CheckedChanged event. Adding an event handler to a Windows Forms control in Visual Studio is easily done by going into designer mode (SHIFT+F7) on the form and double-clicking the control. When adding an event handler to the form, in Solution Explorer (CTRL+;) double-click the form to find the suitable event from the options in the Events button (F4 > Properties > Events). Then code it as follows:
    VB
    Private Sub radioFile_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radioFile.CheckedChanged 
       Try 
          Dim capability As New TwainCapability 
          capability.Information.ContainerType = TwainContainerType.OneValue 
          capability.Information.Type = TwainCapabilityType.ImageTransferMechanism 
                           
          capability.OneValueCapability.ItemType = TwainItemType.Uint16 
          capability.OneValueCapability.Value = CUShort(TwainTransferMechanism.File) 
                           
          _twnSession.SetCapability(capability, TwainSetCapabilityMode.Set) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void radioFile_CheckedChanged(object sender, System.EventArgs e) 
    { 
       try 
       { 
          TwainCapability capability = new TwainCapability(); 
          capability.Information.ContainerType = TwainContainerType.OneValue; 
          capability.Information.Type = TwainCapabilityType.ImageTransferMechanism; 
                           
          capability.OneValueCapability.ItemType = TwainItemType.Uint16; 
          capability.OneValueCapability.Value = (UInt16)TwainTransferMechanism.File; 
                           
          _twnSession.SetCapability(capability, TwainSetCapabilityMode.Set); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
  21. 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