65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 WIALib 在 VB6 应用程序中捕获相机图像

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (5投票s)

2007年5月30日

CPOL
viewsIcon

67332

在 VB6 中使用 WIALib 的示例代码

引言

我使用 WIALib 在 VB6 中遇到了一些困难,无法获得帮助。 事实上,我之前使用的是 Twain 驱动程序,但无法在较新版本的 Microsoft Windows 中使用它们。 较新的设备也不一定为 Microsoft Windows 编写 Twain 驱动程序。

WIALib 是一个新型的 .NET 兼容库,可以用来解决这个问题。 你还会发现这种新的方法更加简洁。

背景

我没有从 Microsoft 获得关于 WIALib 的任何具体资料,但据称它在 MSDN 上可用。 我的版本没有相关内容。 较新的版本可能包含。 你可以尝试 MSDN.com。

创建一个 VB6 项目。 将一个图片框和一个命令按钮添加到窗体中。 在项目 > 引用菜单中,添加 Microsoft Windows Image Acquisition 类型库。

Using the Code

将以下代码行插入到你的项目中。 缩进已经混乱。 请原谅我这一点。

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

关注点

了解 .NET 包装器的设计方式很有趣。 它们使使用变得容易,但对于来自较旧语言的程序员来说,可能很难弄清楚事情。

历史

  • 2007 年 5 月 30 日:第一个版本
© . All rights reserved.