用WIALib捕捉相机图像在VB6应用程序
介绍 我在VB6中使用WIALib有一些困难,无法获得帮助。事实上,我以前用的是Twain驱动程序,但在微软Windows的新版本中却不能使用。较新的设备也不一定有为微软Windows编写的Twain驱动程序。 WIALib是一个新的。net兼容库,可以用来解决这个问题。您还会发现新方法更加清晰。 背景 我还没有从微软得到任何关于WIALib的具体资料,但是很明显,在MSDN上可以找到。我的版本里什么都没有。较新的版本可能有。你可以试试MSDN.com。 创建一个VB6项目。在表单中添加一个图片框和一个命令按钮。来自项目>参考菜单,添加microsoftwindows图像获取类型库。 使用的代码 在项目中插入以下代码行。压痕已经失控。请原谅。 隐藏,收缩,复制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日:第一版 本文转载于:http://www.diyabc.com/frontweb/news2440.html