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 Dim camItem As WIALib.Item Dim img As WIALib.Item
Dim c As WIALib.Collection
Set camList = New WIALib.Wia
If camList.Devices.Count > 0 Then
Set camItem = camList.Create(camList.Devices(0))
Set c = camItem.GetItemsFromUI(SingleImage, ImageTypeColor)
Set img = c(0)
img.Transfer "C:\capture.jpg", False
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 Dim x As Single
Dim y As Single Set pic = New StdPicture
Set pic = LoadPicture("C:\capture.jpg")
asp = pic.Height / pic.Width
If asp < 1 Then
x = 0
y = Picture1.Height * (1 - asp) / 2
Picture1.PaintPicture pic, x, y, Picture1.Width, Picture1.Height * asp, 0, 0
Else
x = Picture1.Width * (1 - (1 / asp)) / 2
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