Results 1 to 34 of 34

Thread: WIA (Windows Image Acquisition) WebCam Sample Project

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    WIA (Windows Image Acquisition) WebCam Sample Project

    Here's a sample VB.NET 2003 project I made that shows how to capture snapshots from a webcam using WIA (Windows Image Acquisition). It detects the available device (tested with a Logitec Webcam), and allows you to grab an image. Being that it is a WIA Sample project, you have to use a WIA enabled device (usually if the device shows up in "Scanners & Cameras" in control panel, it should be WIA enabled).

    The ability to use WIA, you first need to have the WIA Automation Layer, provided in "wiaaut.dll", registered. If you need this, you can download it from Microsoft here: http://www.microsoft.com/downloads/d...DisplayLang=en
    You then need to add a reference to the dll if you wish to use it in a project of yours. It should already be done so in the sample project download at the end.

    ***Note - WIA 2.0 is only supported in Windows XP SP1 or later.

    Sample project code for the procedure to get the available devices (see full code in zip file):
    VB Code:
    1. Private Sub GetDevice()
    2.         Dim MyDevice As WIA.Device
    3.         Dim MyDialog As New WIA.CommonDialogClass
    4.         Try
    5.             'shows selectdevice dialog, if only one device, It automatically selects the device
    6.             '(not tested with two or more devices)
    7.             '**Note - Device Type checks for VideoDeviceType, for webcams, in this sample
    8.             MyDevice = MyDialog.ShowSelectDevice(WIA.WiaDeviceType.VideoDeviceType, False, True)
    9.             If Not MyDevice Is Nothing Then
    10.                 'loops through device properties, only gets the ones we want to display
    11.                 For Each prop As WIA.Property In MyDevice.Properties
    12.                     Select Case prop.Name
    13.                         Case "Manufacturer"
    14.                             lblMfg.Text = prop.Value.ToString
    15.                         Case "Description"
    16.                             lblDesc.Text = prop.Value.ToString
    17.                         Case "Name"
    18.                             lblName.Text = prop.Value.ToString
    19.                         Case "WIA Version"
    20.                             lblWIA.Text = prop.Value.ToString
    21.                         Case "Driver Version"
    22.                             lblDriver.Text = prop.Value.ToString
    23.                     End Select
    24.                 Next
    25.                 'sets MyDevice form level selected device
    26.                 SelectedDevice = MyDevice
    27.             Else
    28.                 lblName.Text = "No WIA Devices Found!"
    29.             End If
    30.         Catch ex As System.Exception
    31.             MessageBox.Show("Problem! " & ex.Message, _
    32.                   "Problem Loading Device", MessageBoxButtons.OK, MessageBoxIcon.Warning, _
    33.                   MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly)
    34.             lblName.Text = "No WIA Devices Found!"
    35.         End Try
    36.     End Sub
    Sample project code to grab the picture from the device (see full code in zip file):
    VB Code:
    1. Private Sub btnGrab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrab.Click
    2.         Dim item As WIA.Item
    3.         Try
    4.             'executes the device's TakePicture command
    5.             item = SelectedDevice.ExecuteCommand(WIA.CommandID.wiaCommandTakePicture)
    6.         Catch ex As System.Exception
    7.             MessageBox.Show("Problem Taking Picture. Please make sure that the camera" & _
    8.                           "is plugged in and is not in use by another application. " & vbCrLf & _
    9.                           "Extra Info:" & ex.Message, "Problem Grabbing Picture", _
    10.                           MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, _
    11.                           MessageBoxOptions.DefaultDesktopOnly)
    12.             Exit Sub
    13.         End Try
    14.         Dim jpegGuid As String
    15.         'retrieves jpegKey from registry, used in getting JPEG format of pic, can be hard coded
    16.         Dim jpegKey As Microsoft.Win32.RegistryKey = _
    17.                  Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("" & _
    18.                  "CLSID\{D2923B86-15F1-46FF-A19A-DE825F919576}\SupportedExtension\.jpg")
    19.         jpegGuid = CType(jpegKey.GetValue("FormatGUID"), String)
    20.         'loops through available formats for the captured item, looking for the JPG format
    21.         For Each format As String In item.Formats
    22.             If (format = jpegGuid) Then
    23.                 'transfers image to an imagefile object
    24.                 Dim imagefile As WIA.ImageFile = CType(item.Transfer(format), WIA.ImageFile)
    25.                 Dim Counter As Integer 'counter in loop appended to filename
    26.                 Dim LoopAgain As Boolean = True
    27.                 'searches directory, gets next available picture name
    28.                 Do Until LoopAgain = False
    29.                     Dim Filename As String = SavePath & "\" & txtPrefix.Text & Counter.ToString & ".jpg"
    30.                     'if file doesnt exist, save the file
    31.                     If Not System.IO.File.Exists(Filename) Then
    32.                         SavedFilePath = Filename
    33.                         imagefile.SaveFile(Filename) 'saves file to disk
    34.                         lblCapName.Text = FormatPath(Filename)
    35.                         lblImageName.Text = txtPrefix.Text & Counter.ToString & ".jpg"
    36.                         picCap.Image = Image.FromFile(Filename) 'loads captured file to picturebox
    37.                         LoopAgain = False
    38.                     End If
    39.                     Counter = Counter + 1
    40.                 Loop
    41.             End If
    42.         Next
    43.         If grpSaved.Enabled = False Then
    44.             grpSaved.Enabled = True
    45.         End If
    46. End Sub
    The code automatically saves the picture to a new filename, so you can grab multiple captures one after another.

    The project so far is only for webcams, but can be extended to do similar things with digital cameras, camcorders, basically any supported WIA device.
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by Hack; Jan 13th, 2006 at 09:56 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width