Click here to Skip to main content
Licence CPOL
First Posted 30 May 2007
Views 36,667
Bookmarked 22 times

Using WIALib to Capture Camera Images from Within VB6 Applications

By | 30 May 2007 | Article
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

License

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

About the Author

uvw

Web Developer

India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionvb6 or .Net Pinmemberkooookies23:47 22 Aug '11  
GeneralTool to set default resulution on Logitech webcams PinmemberSandernes10:35 14 Oct '09  
GeneralOn Vista PinmemberLus Oliveira13:28 5 Feb '09  
GeneralGreat Code PinmemberAndE004:53 12 Jul '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 30 May 2007
Article Copyright 2007 by uvw
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid