Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / Visual Basic

Using WIALib to Capture Camera Images from Within VB6 Applications

Rate me:
Please Sign up or sign in to vote.
2.33/5 (5 votes)
30 May 2007CPOL1 min read 66.5K   22   5
Sample code for using WIALib in VB6

Introduction

I had some difficulty using WIALib in VB6 and could not get help. In fact, I was using Twain drivers earlier and could not use them with newer versions of Microsoft Windows. Newer devices also do not necessarily have Twain drivers written for Microsoft Windows.

WIALib is a new .NET compatible library that can be used to address this problem. You will also find that the new approach is a lot more cleaner.

Background

I have not got any specific material on WIALib from Microsoft but apparently, it is available on MSDN. My version does not have anything on it. Newer versions may have. You can try MSDN.com.

Create a VB6 project. Add a picture box and a command button to the form. From the project > References menu, add Microsoft Windows Image Acquisition Type library.

Using the Code

Insert the following lines of code in your project. Indentation has gone haywire. Excuse me for that.

VB.NET
Private Sub Command1_Click()
   On Error Resume Next
   Dim camList As WIALib.Wia 'all the devices connected
   Dim camItem As WIALib.Item 'One of the devices
   Dim img As WIALib.Item 
   'Note that WIALib.Item can wrapper for many types of objects
   'We are using it as a device and as an image, here
   Dim c As WIALib.Collection
   'Note that c is not a normal VB collection but specific to WIALib
'Initialize
   Set camList = New WIALib.Wia
   If camList.Devices.Count > 0 Then
      'MsgBox camList.Devices.Count & "devices attached"

      'Connect to the first device. You could use other devices as well.
      Set camItem = camList.Create(camList.Devices(0))

      'MsgBox camItem.FullName & " is of type " & camItem.ItemType
      'Let the driver popup its dialog. Read one of the images selected by user

      Set c = camItem.GetItemsFromUI(SingleImage, ImageTypeColor)

      'We have selected single image. so, we will read the first (and the only one)

      Set img = c(0)

      'MsgBox img.FullName & " is of type " & img.ItemType

      'save the image to a file (application buffer).

      img.Transfer "C:\capture.jpg", False

      'Display it from application buffer

      displayImage
   Else
      MsgBox "No WIA compatible device attached"

   End If
 
   If Err <> 0 Then
      MsgBox Err.Description
      Err.Clear
   End If
End Sub
 
Private Sub displayImage()
Dim pic As StdPicture
Dim asp As Single 'aspect ratio of picture
Dim x As Single
Dim y As Single 'offsets to center the picture
Set pic = New StdPicture
Set pic = LoadPicture("C:\capture.jpg")
asp = pic.Height / pic.Width

 'use aspect ratio to correct the picture size.
If asp < 1 Then
   x = 0
   y = Picture1.Height * (1 - asp) / 2
   '=half of difference in height
   Picture1.PaintPicture pic, x, y, Picture1.Width, Picture1.Height * asp, 0, 0
Else
   x = Picture1.Width * (1 - (1 / asp)) / 2
   '=half of difference in width
   y = 0
   Picture1.PaintPicture pic, x, y, Picture1.Width / asp, Picture1.Height, 0, 0

End If
End Sub

Points of Interest

It is interesting to know how .NET wrappers are designed. They make things easy to use but for a programmer coming from older languages, it may be very hard to figure out things.

History

  • 30th May, 2007: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
uvw
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWIALIB Pin
soft9919-Jun-15 6:14
soft9919-Jun-15 6:14 
Questionvb6 or .Net Pin
kooookies22-Aug-11 23:47
kooookies22-Aug-11 23:47 
GeneralTool to set default resulution on Logitech webcams Pin
Sandernes14-Oct-09 10:35
Sandernes14-Oct-09 10:35 
GeneralOn Vista Pin
Luis Oliveira 19665-Feb-09 13:28
Luis Oliveira 19665-Feb-09 13:28 
GeneralGreat Code Pin
AndE0012-Jul-08 4:53
AndE0012-Jul-08 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.