Acquiring Still Image From Streaming Video

Take the following steps to create and run a program that acquires a still image from a WIA video stream.

  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 Still Image From Streaming Video" 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\Dotnet\Win32" folder and select the following DLL:

    • Leadtools.Wia.dll

    Click the Select button and then press the OK button to add the above DLL to the application.

  6. Go to the toolbox (View->Toolbox) and Drag and drop 4 button controls to the top of the form and set the following properties for them:

Text Name
Select Video Device btnSelectVideoDevice
Start Video Preview btnStartVideoPreview
Acquire Image From Video btnAcquireImageFromVideo
Stop Video Preview btnStopVideoPreview
  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.Wia 
                     
         
    using Leadtools.Wia; 
                     
         

  2. Declare the following private variable:

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

  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 
          session = new WiaSession 
          session.Startup(WiaVersion.Version1) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
                     
         
    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); 
       } 
    } 
                     
         

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

  5. Add an event handler to the btnSelectVideoDevice Click event and code it as follows:

    C#
    VB
    Private Sub btnSelectVideoDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectVideoDevice.Click 
       Try 
          session.SelectDeviceDlg(Me, WiaDeviceType.StreamingVideo, WiaSelectSourceFlags.NoDefault) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
                     
         
    private void btnSelectVideoDevice_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          session.SelectDeviceDlg(this, WiaDeviceType.StreamingVideo, WiaSelectSourceFlags.NoDefault); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
                     
         

  6. Add an event handler to the btnStartVideoPreview Click event and code it as follows:

    C#
    VB
    Private Sub btnStartVideoPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartVideoPreview.Click 
       Try 
          session.StartVideoPreview(Me, false) 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
                     
         
    private void btnStartVideoPreview_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          session.StartVideoPreview(this, false); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
                     
         

  7. Add an event handler to the btnAcquireImageFromVideo Click event and code it as follows:

    C#
    VB
    Private Sub btnAcquireImageFromVideo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcquireImageFromVideo.Click 
       Try 
          Dim strMsg As String 
          Dim takenPictureFileName As String = session.AcquireImageFromVideo() 
          strMsg = String.Format("Acquired still image was saved to:{0}{1}", Constants.vbLf, takenPictureFileName) 
          MessageBox.Show(strMsg, "Acquired still image path") 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub 
                     
         
    private void btnAcquireImageFromVideo_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          string strMsg; 
          string takenPictureFileName = session.AcquireImageFromVideo(); 
          strMsg = string.Format("Acquired still image was saved to:\n{0}", takenPictureFileName); 
          MessageBox.Show(strMsg, "Acquired still image path"); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
                     
         

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

    C#
    VB
    Private Sub btnStopVideoPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopVideoPreview.Click 
       Try 
          session.EndVideoPreview() 
       Catch ex As Exception 
          MessageBox.Show(ex.Message) 
       End Try 
    End Sub    
                     
         
    private void btnStopVideoPreview_Click(object sender, System.EventArgs e) 
    { 
       try 
       { 
          session.EndVideoPreview(); 
       } 
       catch (Exception ex) 
       { 
          MessageBox.Show(this, ex.Message); 
       } 
    } 
                     
         

  9. 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