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
  1. 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
  1. 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; 
                     
         

  2. Declare the following private variable:

    VB
    Private WithEvents _twnSession As TwainSession 
                     
         

    C#
    private TwainSession _twnSession; 
                     
         

  3. 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 
       Try 
       	  Dim myLicenseFile As String = "C:\LEADTOOLS 19\Common\License\leadtools.lic" 
          Dim myDeveloperKey As String = System.IO.File.ReadAllText("C:\LEADTOOLS 19\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 19\Common\License\leadtools.lic"; 
          string myDeveloperKey = System.IO.File.ReadAllText(@"C:\LEADTOOLS 19\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); 
       } 
    } 
                     
         

  4. Add an event handler to the Form1 Closing event and 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); 
       } 
    } 
                     
         

  5. Add an event handler to the _twnSession AcquirePage event and 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; 
    } 
                     
         

  6. Add an event handler to the buttonAcquire Click event and 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); 
       } 
    } 
                     
         

  7. Add an event handler to the buttonSelectSource Click event and 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); 
       } 
    } 
                     
         

  8. Add an event handler to the buttonSaveTemplateFile Click event and 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); 
       } 
    } 
                     
         

  9. Add an event handler to the buttonLoadTemplateFile Click event and 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); 
       } 
    } 
                     
         

  10. Add an event handler to the radioNative CheckedChanged event and 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); 
       } 
    } 
                     
         

  11. Add an event handler to the radioMemory CheckedChanged event and 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); 
       } 
    } 
                     
         

  12. Add an event handler to the radioFile CheckedChanged event and 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); 
       } 
    } 
                     
         

  13. Build and run the program to test it.
Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document