From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Example Code

IMAQdx Snap Example in VB.NET

Code and Documents

Attachment

Overview

Ever wanted to use the IMAQdx drivers with VB .NET and perform a snap image acquisition? Well you have come to the right place! It has been a long time waiting for an update to the legacy ActiveX method so I created my own. Enjoy!

Description

This VB .NET code implements a snap on a USB camera using the IMAQdx drivers. The code was written for Visual Studio 2010 with additional Measurement Studio plugin. Any USB camera can run this code and perform a snap in Visual Studio.

app.png

Steps to Implement or Execute Code

  1. Open the Visual Studio 2010 project located in the zip file attached.
  2. Ignore the warning asking to use a newer version of .NET drivers and press cancel:
    • warning.png
  3. While open you can simply run the code by pressing the run arrow in your toolbar.
  4. The code should compile and search for available cameras.
  5. Select the camera entry you want to use and press start
  6. An image should be acquired by the camera and displayed in the central image viewer.
  7. Pressing quit exits the application

Requirements

Software

Visual Studio 2010

Measurement Studio 2010

IMAQdx 4.0

Hardware

USB Camera

VB Snippet

Imports System.Collections.Generic

Imports System.ComponentModel

Imports System.Data

Imports System.Drawing

Imports System.Text

Imports System.Windows.Forms

Imports NationalInstruments.Vision

Imports NationalInstruments.Vision.Acquisition.Imaqdx

Namespace Snap

    Partial Public Class Form1

        Inherits Form

        Private _session As ImaqdxSession

        Public Sub New()

            InitializeComponent()

            startButton.Enabled = True

            ' Enumerate cameras and populate control

            Dim cameraList As ImaqdxCameraInformation() = ImaqdxSystem.GetCameraInformation(True)

            If cameraList.Length > 0 Then

                cameraComboBox.Items.Clear()

                For Each camInfo As ImaqdxCameraInformation In cameraList

                    cameraComboBox.Items.Add(camInfo.Name)

                Next

                cameraComboBox.SelectedIndex = 0

            End If

            ' Create image and attach to image viewer

            Dim image As New NationalInstruments.Vision.VisionImage()

            Me.ImageViewer.Attach(image)

        End Sub

        Private Sub startButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles startButton.Click

            ' Open camera

            _session = New ImaqdxSession(cameraComboBox.Text)

            ' Snap an image

            _session.Snap(Me.ImageViewer.Image)

            ' Close the camera

            _session.Close()

        End Sub

        Private Sub quitButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles quitButton.Click

            Me.Close()

            Application.[Exit]()

        End Sub

        Private Sub InitializeComponent()

            Me.cameraComboBox = New System.Windows.Forms.ComboBox()

            Me.startButton = New System.Windows.Forms.Button()

            Me.quitButton = New System.Windows.Forms.Button()

            Me.ImageViewer = New NationalInstruments.Vision.WindowsForms.ImageViewer()

            Me.SuspendLayout()

            '

            'cameraComboBox

            '

            Me.cameraComboBox.FormattingEnabled = True

            Me.cameraComboBox.Location = New System.Drawing.Point(21, 457)

            Me.cameraComboBox.Name = "cameraComboBox"

            Me.cameraComboBox.Size = New System.Drawing.Size(115, 21)

            Me.cameraComboBox.TabIndex = 6

            Me.cameraComboBox.Text = "cam0"

            '

            'startButton

            '

            Me.startButton.Location = New System.Drawing.Point(206, 454)

            Me.startButton.Name = "startButton"

            Me.startButton.Size = New System.Drawing.Size(85, 25)

            Me.startButton.TabIndex = 7

            Me.startButton.Text = "Start"

            Me.startButton.UseVisualStyleBackColor = True

            '

            'quitButton

            '

            Me.quitButton.Location = New System.Drawing.Point(559, 454)

            Me.quitButton.Name = "quitButton"

            Me.quitButton.Size = New System.Drawing.Size(82, 25)

            Me.quitButton.TabIndex = 8

            Me.quitButton.Text = "Quit"

            Me.quitButton.UseVisualStyleBackColor = True

            '

            'ImageViewer

            '

            Me.ImageViewer.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle

            Me.ImageViewer.Location = New System.Drawing.Point(28, 20)

            Me.ImageViewer.Name = "ImageViewer"

            Me.ImageViewer.Size = New System.Drawing.Size(612, 402)

            Me.ImageViewer.TabIndex = 9

            '

            'Form1

            '

            Me.ClientSize = New System.Drawing.Size(667, 490)

            Me.Controls.Add(Me.ImageViewer)

            Me.Controls.Add(Me.quitButton)

            Me.Controls.Add(Me.startButton)

            Me.Controls.Add(Me.cameraComboBox)

            Me.Name = "Form1"

            Me.Text = "Test"

            Me.ResumeLayout(False)

        End Sub

        Private WithEvents cameraComboBox As System.Windows.Forms.ComboBox

        Private WithEvents startButton As System.Windows.Forms.Button

        Private WithEvents quitButton As System.Windows.Forms.Button

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        End Sub

        Friend WithEvents ImageViewer As NationalInstruments.Vision.WindowsForms.ImageViewer

        Private Sub ImageViewer_RoiChanged_1(ByVal sender As System.Object, ByVal e As NationalInstruments.Vision.WindowsForms.ContoursChangedEventArgs) Handles ImageViewer.RoiChanged

        End Sub

    End Class

End Namespace


Larry Colvin
Associate Principal Engineer
Dyson Technology Ltd.

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
Larry.C
Active Participant
Active Participant
on

BTW, I am no VB expert and have probably made a number of mistakes. Please feel free to let me know if there are any improvements I can make!


Larry Colvin
Associate Principal Engineer
Dyson Technology Ltd.

CertifiNATEtion
Member
Member
on

Hi Larry,

I tried to run this program in Visual Studio 2010 and 2012 on two systems but ran into the following error. I have VDM 2013 installed, any thoughts on what could be causing this? Also this might need some more testing on other systems.

Error12Reference required to assembly 'NationalInstruments.Common, Version=8.7.35.131, Culture=neutral, PublicKeyToken=18cbae0f9955702a' containing the implemented interface 'NationalInstruments.ISupportSynchronizationContext'. Add one to your project.C:\Users\nletchfo\Downloads\imaqdx.net\IMAQdx VB.NET\Form1.vb1429IMAQdx VB.NET

The error occurs at _Session = New ImaqdxSession(cameraComboBox.Text)

National Instruments
Staff Certification Engineer
Larry.C
Active Participant
Active Participant
on

Hi Nathanael,

I remember seeing that error. Have you checked your visual studio compiler is using ".NET 4" and not ".NET 4 Client Profile"?

I seem to think this fixed the problem for me.

Let me know how you get on!

Kind Regards,

Larry


Larry Colvin
Associate Principal Engineer
Dyson Technology Ltd.

Larry.C
Active Participant
Active Participant
on

Also, wanted to make sure you have also ignored the error message when you initially open the project as shown in the description above. Visual studio tries to force you to use a newer library instead of the .NET 3.5 and asks you to update. Make sure to cancel this message.


Larry Colvin
Associate Principal Engineer
Dyson Technology Ltd.

eMoss
Member
Member
on

I have an issue with the load of of the image when running the snap example. When the start button is clicked, there is a good 6-8 second delay for the image to appear. I've repeated this on two machines and both were slow. Any suggestions?

I am using visual studio 2010 and have NI Vision Development 2014 (this also installs NI-IMAQdx)

Contributors