Wia Scanning

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am attempting to scan in multiple pages from an ADF on a variety if scanners (Mostly MFCs). I have been fighting this for about a week with not much success. I do have it working under twain, however, each page is a separate tif file. I would like to
use WIA and have them combined. I found some code, but it was written in a previous version and generates an error when I run it in 2010.
The error is :Value does not fall within the expected range.
I highlighted where the error is taking place

<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Private <span style="color:Blue; Sub ScanButton_Click(sender <span style="color:Blue; As System.Object, e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles ScanButton.Click
Regkey = Registry.LocalMachine.OpenSubKey(<span style="color:#A31515; "SoftwareRdyDocs", <span style="color:Blue; True)
<span style="color:Blue; If Regkey.GetValue(<span style="color:#A31515; "ScanType") = <span style="color:#A31515; "Wia" <span style="color:Blue; Then

<span style="color:Green; Choose Scanner
<span style="color:Blue; Dim class1 <span style="color:Blue; As <span style="color:Blue; New CommonDialogClass()
<span style="color:Blue; Dim d <span style="color:Blue; As Device = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, <span style="color:Blue; True, <span style="color:Blue; False)
<span style="color:Blue; If d <span style="color:Blue; IsNot <span style="color:Blue; Nothing <span style="color:Blue; Then
<span style="color:Blue; Me.DeviceID = d.DeviceID
<span style="color:Blue; Else
<span style="color:Green; no scanner chosen
<span style="color:Blue; Return
<span style="color:Blue; End <span style="color:Blue; If



<span style="color:Blue; Dim WiaCommonDialog <span style="color:Blue; As WIA.CommonDialog = <span style="color:Blue; New CommonDialogClass()

<span style="color:Blue; Dim hasMorePages <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; True
<span style="color:Blue; Dim x <span style="color:Blue; As <span style="color:Blue; Integer = 0
<span style="color:Blue; Dim numPages <span style="color:Blue; As <span style="color:Blue; Integer = 0
<span style="color:Blue; While hasMorePages
<span style="color:Green; Create DeviceManager
<span style="color:Blue; Dim manager <span style="color:Blue; As DeviceManager = <span style="color:Blue; New DeviceManagerClass()
<span style="color:Blue; Dim WiaDev <span style="color:Blue; As Device = <span style="color:Blue; Nothing
<span style="color:Blue; For <span style="color:Blue; Each info <span style="color:Blue; As DeviceInfo <span style="color:Blue; In manager.DeviceInfos
<span style="color:Blue; If info.DeviceID = <span style="color:Blue; Me.DeviceID <span style="color:Blue; Then
<span style="color:Blue; Dim infoprop <span style="color:Blue; As WIA.Properties = <span style="color:Blue; Nothing
infoprop = info.Properties

<span style="color:Green; connect to scanner
WiaDev = info.Connect()


<span style="color:Blue; Exit <span style="color:Blue; For
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next

<span style="color:Green; Start Scan

<span style="color:Blue; Dim img <span style="color:Blue; As WIA.ImageFile = <span style="color:Blue; Nothing

<span style="color:Blue; Dim Item <span style="color:Blue; As WIA.Item = TryCast(WiaDev.Items(1), WIA.Item)

<span style="color:Green; Try
<span style=<span style="color:#A31515; "text-decoration:underline"> img = <span style="color:Blue; DirectCast(WiaCommonDialog.ShowTransfer(Item, wiaFormatTIFF, <span style="color:Blue; False), ImageFile) <span style="color:Green; error occurs here

<span style="color:Green; process image:
<span style="color:Green; one would do image processing here
<span style="color:Green;
<span style="color:Green; Save to file
<span style="color:Blue; Dim varImageFileName <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "c:test" & x.ToString() & <span style="color:#A31515; ".tiff"
<span style="color:Blue; If File.Exists(varImageFileName) <span style="color:Blue; Then
<span style="color:Green; file exists, delete it
File.Delete(varImageFileName)
<span style="color:Blue; End <span style="color:Blue; If
img.SaveFile(varImageFileName)
numPages += 1

img = <span style="color:Blue; Nothing
<span style="color:Green; Catch ex As Exception
<span style="color:Green; MessageBox.Show("Error: " & ex.Message)
<span style="color:Green; Finally
Item = <span style="color:Blue; Nothing
<span style="color:Green; determine if there are any more pages waiting
<span style="color:Blue; Dim documentHandlingSelect <span style="color:Blue; As WIA.<span style="color:Blue; Property = <span style="color:Blue; Nothing
<span style="color:Blue; Dim documentHandlingStatus <span style="color:Blue; As WIA.<span style="color:Blue; Property = <span style="color:Blue; Nothing
<span style="color:Blue; For <span style="color:Blue; Each prop <span style="color:Blue; As WIA.<span style="color:Blue; Property <span style="color:Blue; In WiaDev.Properties
<span style="color:Blue; If prop.PropertyID = WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT <span style="color:Blue; Then
documentHandlingSelect = prop
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; If prop.PropertyID = WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS <span style="color:Blue; Then
documentHandlingStatus = prop


<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Next

hasMorePages = <span style="color:Blue; False
<span style="color:Green; assume there are no more pages
<span style="color:Blue; If documentHandlingSelect <span style="color:Blue; IsNot <span style="color:Blue; Nothing <span style="color:Blue; Then
<span style="color:Green; may not exist on flatbed scanner but required for feeder
<span style="color:Green; check for document feeder

<span style="color:Green; If (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER <> 0) Then
<span style="color:Green; hasMorePages = WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY
<span style="color:Green; End If

<span style="color:Blue; If (Convert.ToUInt32(documentHandlingSelect.Value) <span style="color:Blue; And WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) <> 0 <span style="color:Blue; Then
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.Value) <span style="color:Blue; And WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) <> 0)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; If
x += 1
<span style="color:Green; End Try
<span style="color:Blue; End <span style="color:Blue; While



Regkey.Close()


<span style="color:Blue; ElseIf Regkey.GetValue(<span style="color:#A31515; "ScanType") = <span style="color:#A31515; "Twain" <span style="color:Blue; Then
<span style="color:Blue; Dim <span style="color:Blue; Count <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim cntmsg <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Count = 0
<span style="color:Blue; Me.ScanButton.Enabled = <span style="color:Blue; False
<span style="color:Blue; Me.ScanButton.Visible = <span style="color:Blue; False
<span style="color:Blue; Try
S = TwainHandler.ScanImages()
<span style="color:Blue; For <span style="color:Blue; Each k <span style="color:Blue; In S
<span style="color:Blue; Count = <span style="color:Blue; Count + 1
<span style="color:Blue; Next
<span style="color:Blue; If <span style="color:Blue; Count > 0 <span style="color:Blue; Then
cntmsg = <span style="color:#A31515; "You have scanned " & <span style="color:Blue; Count.ToString & <span style="color:#A31515; " Documents"
MessageBox.Show(cntmsg, <span style="color:#A31515; "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
<span style="color:Blue; Me.ReviewButton.Enabled = <span style="color:Blue; True
<span style="color:Blue; Me.ReviewButton.Visible = <span style="color:Blue; True
<span style="color:Blue; Me.ReviewButton.Focus()
<span style="color:Blue; Else
MessageBox.Show(<span style="color:#A31515; "No files were scanned, Please verify your scanner is connected", <span style="color:#A31515; "Failed", MessageBoxButtons.OK, MessageBoxIcon.Information)
<span style="color:Blue; Me.ScanButton.Enabled = <span style="color:Blue; True
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
Debug.Print(<span style="color:#A31515; "No scanner found ")
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; If

<span style="color:Blue; End <span style="color:Blue; Sub
[/code]

Thank you for any assistance you can provide in either helping me identify the problem here or providing an example of working code somewhere else.
Regards.



View the full article
 
Back
Top