Acquiring an Image

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

  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 "Acquiring an Image" 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\DotNet4\Win32" folder and select the following DLLs:

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

    Click the Select button and then press 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 a 3 instance 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
File radioFile False
  1. Go to the toolbox (View->Toolbox) and Drag and drop a 4 instance of 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 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.Controls.WinForms 
    using Leadtools; 
    using Leadtools.Twain; 
    using Leadtools.Codecs; 
    using Leadtools.Controls.WinForms; 

  2. Declare the following private variable:

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

  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 
          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 
       { 
          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 twnSession AcquirePage event and code it as follows:

    C#
    VB
    Private Sub twnSession_AcquirePage(ByVal sender As Object, ByVal e As TwainAcquirePageEventArgs) Handles twnSession.AcquirePage 
       RasterImageViewer1.Image = e.Image 
    End Sub 
    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:

    C#
    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 
    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:

    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); 
       } 
    } 

  8. Add an event handler to the buttonSaveTemplateFile Click event and code it as follows:

    C#
    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 
    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:

    C#
    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 
    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:

    C#
    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 
    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:

    C#
    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 
    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:

    C#
    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 
    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.
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Imaging, Medical, and Document
Click or drag to resize