65.9K
CodeProject is changing. Read more.
Home

Using WIALib to Capture Camera Images from Within VB6 Applications

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (5 votes)

May 30, 2007

CPOL
viewsIcon

67010

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.

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