Acquiring an Image From WIA Source

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

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

    • Leadtools.dll
    • Leadtools.Wia.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 the "LEADTOOLS 20\Bin\DotNet4\Win32" folder and then click Open and then click OK.

  7. Go to the toolbox (View->Toolbox) and Drag and drop a 2 instance of Button control to the top of the form and set the following properties for them:

    Text Name
    Acquire btnAcquire
    Select Source btnSelectSource
  8. 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.Wia 
    Imports Leadtools.Codecs 
    Imports Leadtools.Controls.WinForms 
    C#
    using Leadtools; 
    using Leadtools.Wia; 
    using Leadtools.Codecs; 
    using Leadtools.Controls.WinForms; 

  9. Declare the following private variable:

    VB
    Private WithEvents session As WiaSession 
    C#
    private WiaSession session; 

  10. 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 
          session = new WiaSession 
          session.Startup(WiaVersion.Version1) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
    C#
    private void Form1_Load(object sender, System.EventArgs e) 
    { 
       try 
       { 
          session = new WiaSession(); 
          session.Startup(WiaVersion.Version1); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  11. 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 
          session.Shutdown() 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
    C#
    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
       try 
       { 
          session.Shutdown(); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

  12. Add the below code to handle the AcquireEvent event:

    VB
    Private Sub session_AcquireEvent(ByVal sender As Object, ByVal e As WiaAcquireEventArgs) Handles session.AcquireEvent 
      If Not e.Image Is Nothing Then 
         RasterImageViewer1.Image = e.Image 
      End If 
    End Sub 
    C#
    private void session_AcquireEvent(object sender, WiaAcquireEventArgs e) 
    { 
      if (e.Image != null) 
      { 
         rasterImageViewer1.Image = e.Image; 
      } 
    } 

  13. Add an event handler to the btnAcquire Click event and code it as follows:

    VB
    Private Sub btnAcquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcquire.Click 
       Try 
          session.Acquire(Me, Nothing, WiaAcquireFlags.ShowUserInterface Or WiaAcquireFlags.UseCommonUI) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
    C#
    private void btnAcquire_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          session.AcquireEvent += new EventHandler<WiaAcquireEventArgs>(session_AcquireEvent); 
          session.Acquire(this, null, WiaAcquireFlags.ShowUserInterface |   WiaAcquireFlags.UseCommonUI); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 

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

    VB
    Private Sub btnSelectSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectSource.Click 
       Try 
          session.SelectDeviceDlg(Me, WiaDeviceType.Default, WiaSelectSourceFlags.NoDefault) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
    C#
    private void btnSelectSource_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          session.SelectDeviceDlg(this, WiaDeviceType.Default, WiaSelectSourceFlags.NoDefault); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, 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