Trouble automating scanning using WIA and VB.NET
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: Trouble automating scanning using WIA and VB.NET

Hybrid View

  1. #1
    Join Date
    Nov 2012
    Posts
    10

    Unhappy Trouble automating scanning using WIA and VB.NET

    Hello Stack overflow !

    I'm the ultimate beginner in programming. I have some experience in php and vba, doing my own scripts as I need them, especially in excel.

    Recently, for a project at works, I need to be able to scan **AUTOMATICALLY** (say every 2 minuteS) from **multiple scanners** (say 2 for starters) **both connected to the same computer**.
    I decided to use this project as a start point for me to get a feeling of Visual Basic.
    So here we go, I installed visual studio express 2010 and started writing my script trying to find here and there bits of codes that could help me. I found that WIA could help with that (Twain could as well but it seems much more obscure to the newbie I am)

    Anyway, I finally came up with an app that is able to automatically scan at the set interval when only one scanner is connected. The trouble arrives when I connect more than one scanner. then, the first scan occurs correctly (Scanner 1 scans, then scanner 2 scans), but when the second scan is supposed to start, nothing happens and the scanners become inaccessible (busy).
    I though maybe I forgot to "release" or "disconnect" the last scanner used. Or maybe, something remains in the scanner's buffer memory ?

    I have been stuck on this issue for the last 3 days and don't know how to make it work.

    here is the function that scans : (i don't paste the rest as it is the UI and folder management)

    Code:
         Public Sub scannerloop()
    
            'format constants
            Const wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
            Const wiaFormatPNG = "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
            Const wiaFormatGIF = "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
            Const wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
            Const wiaFormatTIFF = "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"
    
            
            'file format
            Dim fileformat As String
            If Me.FileExt.SelectedItem = "TIF" Then fileformat = wiaFormatTIFF
            If Me.FileExt.SelectedItem = "JPEG" Then fileformat = wiaFormatJPEG
            If Me.FileExt.SelectedItem = "BMP" Then fileformat = wiaFormatBMP
            If Me.FileExt.SelectedItem = "PNG" Then fileformat = wiaFormatPNG
            If Me.FileExt.SelectedItem = "GIF" Then fileformat = wiaFormatGIF
    
            'colors
            Dim colorcode As Integer
            If Me.Colorbox.SelectedItem = "Black and white" Then colorcode = 4
            If Me.Colorbox.SelectedItem = "Greyscale" Then colorcode = 2
            If Me.Colorbox.SelectedItem = "Colour" Then colorcode = 1
    
            'Resolution
            Dim dpi As Integer
            dpi = Me.dpiBox.SelectedItem
            Dim horizextent = dpi * 8.2
            Dim vertextent = dpi * 11.6
    
    
            Dim j As String = 1
            Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
    
            For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
    
                If DeviceManager1.DeviceInfos(i).Type = 1 Then  'Select only scanners, not webcams etc...
    
                    'startpoint to calculate how long it is to scan
                    Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
    
                    'Directory + file
                    Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                    Form2.CurrentActionLabel.Text = "Scanning from scanner #" & j
    
    
                    Dim Scanner As WIA.Device = DeviceManager1.DeviceInfos(i).connect
    
    
                    If IsNothing(Scanner) Then
                        Log(Me.logfilename, Now & " | Scanner #" & j & " not found")
                    Else
                        Try
                            Dim Img As WIA.ImageFile
    
                            With Scanner.Items(1)
                                .Properties("6146").Value = colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
                                .Properties("6147").Value = dpi  'dots per inch/horizontal
                                .Properties("6148").Value = dpi 'dots per inch/vertical
                                .Properties("6149").Value = 0 'x point where to start scan
                                .Properties("6150").Value = 0 'y-point where to start scan
    
                                'Following is A4 paper size. (Not 100% accurate because real A4 Ht errors)
                                .Properties("6151").Value = horizextent 'horizontal exent DPI x inches wide
                                .Properties("6152").Value = vertextent 'vertical extent DPI x inches tall
                                '  .Properties("4104").Value = 8 'bits per pixel
    
                            End With
    
                            'transfer image
                            Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.
    
                            'kill previous file if exists to avoid errors
                            If System.IO.File.Exists(targetdir) = True Then
                                Kill(targetdir)
                            End If
    
                            Img.SaveFile(targetdir)
    
                            'last scan
                            Form2.LastFileLabel.Text = "\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                            Form2.LastScanLabel.Text = Now
    
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        Finally
    
                            Scanner = Nothing
                        End Try
                    End If
    
                    'End time for the scan
                    Dim ScanEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
                    'log
                    Log(Me.logfilename, Now & " | Scanner #" & j & " | Scanned " & targetdir & " | duration: " & (ScanEnd - ScanStart))
    
                    j = j + 1
    
    
    
            Next
            DeviceManager1 = Nothing
    
    
            Me.CurrFileIndex = Me.CurrFileIndex + 1
    
            Me.ScanCount = Me.ScanCount + 1
            Me.NextScan = DateAdd("n", Me.IntervalBox.Value, Now)
    
            Form2.ScanCountLabel.Text = Me.ScanCount
            Form2.NextScanLabel.Text = Me.NextScan
            Form2.CurrentActionLabel.Text = "Waiting..."
    
            'Increment next file index and update in config file
            Me.FileIndexBox.Value = Me.CurrFileIndex
            SaveCfg()
    
        End Sub
    Please be indulgent with me, I am aware that the code is probably a nightmare for programming pros with lots of bad stuff, but it is literally my first VB program, and I am eager to learn.



    So basically, the rest of the program is a form where I enter the target directory for the scan, the filenames, resolution etc, and when I click on 'start scanning', it
    - runs scannerloop one first time
    - starts a 'scantimer' which launches scannerloop each time it ticks.
    I do not want any dialog to appear to as me which scanner to select or which parameters to use as this program is supposed to run over several weeks in order to scan hundreds of images without anyone to attend it.

    As I said, it works perfectly with 1 scanner (files created as expected, logfile updated, etc) but as soon as I have 2 scanners, only the first scan works and then, when scanner#1 is supposed to start scanning, it doesn't and the led of scanner#2 starts blinking (as if it was scanning, but it's not scanning)

    I hope someone will be able to help me.

    Thanks in advance.

    Vince

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Trouble automating scanning using WIA and VB.NET

    Try recording a script that scans from two scanners, and goes back to scan the first one. Then look at the code that was generated. It should help.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    I just tried to add a for loop to make it scan from both scanners several times :

    Code:
           
    Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
     For k = 1 To 3
                Dim j As String = 1
                For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
    [...]
              Next i
            Next k
            DeviceManager1 = Nothing


    That showed that the first occurence of the loop works (scans once from each scanner) but that's it, the scanners never scan the second time and start blinking, so basically exactly the same problem.
    I also tried to include the Devicemanager declaration in the new loop :

    Code:
          
     For k = 1 To 3 
    Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
                Dim j As String = 1
                For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
    [...]
              Next i
            DeviceManager1 = Nothing
            Next k
    but it did not change anything.










    The next thing I did wat to log the events within the loop so that I can know where exactly things stop :
    Code:
            Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
            Dim j As String = 1
    
    
            For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
                If DeviceManager1.DeviceInfos(i).Type = 1 Then  'Select only scanners, not webcams etc...
    
                    'startpoint to calculate how long it is to scan
                    Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
    
                    'Directory + file
                    Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                    Form2.CurrentActionLabel.Text = "Scanning from scanner #" & j
    
    
                    Dim Scanner As WIA.Device = DeviceManager1.DeviceInfos(i).connect
    
    
                    If IsNothing(Scanner) Then
                        Log(Me.logfilename, Now & " | Scanner #" & j & " not found")
                    Else
                        Try
                            Dim Img As WIA.ImageFile
    
                            'log
                            Log(Me.logfilename, Now & " | Scanner #" & j & " | Img initialized")
    
                            With Scanner.Items(1)
                                .Properties("6146").Value = colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
                                .Properties("6147").Value = dpi  'dots per inch/horizontal
                                .Properties("6148").Value = dpi 'dots per inch/vertical
                                .Properties("6149").Value = 0 'x point where to start scan
                                .Properties("6150").Value = 0 'y-point where to start scan
    
                                'Following is A4 paper size. (Not 100% accurate because real A4 Ht errors)
                                .Properties("6151").Value = horizextent 'horizontal exent DPI x inches wide
                                .Properties("6152").Value = vertextent 'vertical extent DPI x inches tall
                                '  .Properties("4104").Value = 8 'bits per pixel
    
                            End With
    
                            'log
                            Log(Me.logfilename, Now & " | Scanner #" & j & " | properties initialized")
    
                            'transfer image
                            Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.
    
                            'log
                            Log(Me.logfilename, Now & " | Scanner #" & j & " |Transfer done")
    
                            'kill previous file if exists to avoid errors
                            If System.IO.File.Exists(targetdir) = True Then
                                Kill(targetdir)
                                'log
                                Log(Me.logfilename, Now & " | Scanner #" & j & " | deleted existing " & targetdir)
    
                            End If
    
                            Img.SaveFile(targetdir)
                            'log
                            Log(Me.logfilename, Now & " | Scanner #" & j & " | saved " & targetdir)
    
                            'last scan
                            Form2.LastFileLabel.Text = "\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                            Form2.LastScanLabel.Text = Now
    
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        Finally
    
                            Scanner = Nothing
                        End Try
                    End If
    
                    'End time for the scan
                    Dim ScanEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
                    'log
                    Log(Me.logfilename, Now & " | Scanner #" & j & " | Scanned " & targetdir & " | duration: " & (ScanEnd - ScanStart))
    
                    j = j + 1
    
                End If
    
            Next i

    and here is the logfile generated :
    Scan starts 29/11/2012 9:24:31 AM | Interval :Start scanning with 5 min | Res:100 DPI |
    29/11/2012 9:24:31 AM | Scanner #1 | Img initialized
    29/11/2012 9:24:31 AM | Scanner #1 | properties initialized
    29/11/2012 9:24:49 AM | Scanner #1 |Transfer done
    29/11/2012 9:24:49 AM | Scanner #1 | saved C:\__2\scans\Scanner1\S1_img_1.TIF
    29/11/2012 9:24:49 AM | Scanner #1 | Scanned C:\__2\scans\Scanner1\S1_img_1.TIF | duration: 18
    29/11/2012 9:24:49 AM | Scanner #2 | Img initialized
    29/11/2012 9:24:49 AM | Scanner #2 | properties initialized
    29/11/2012 9:25:08 AM | Scanner #2 |Transfer done
    29/11/2012 9:25:08 AM | Scanner #2 | saved C:\__2\scans\Scanner2\S2_img_1.TIF
    29/11/2012 9:25:08 AM | Scanner #2 | Scanned C:\__2\scans\Scanner2\S2_img_1.TIF | duration: 19
    29/11/2012 9:25:08 AM | Scanner #1 | Img initialized
    29/11/2012 9:25:08 AM | Scanner #1 | properties initialized


    it appears that things go wrong at this line :
    Code:
    Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.






    It looks like WIA is happy to switch from scanner 1 to 2 but refuses to come back to scanner 1 for the next round. also, I should precise, when the second scan is supposed to occur, scanner #2 blinks (and not 1 which surprises me).
    Is it possible that scanner#2 is selected as "default scanner" or something like that and if so, is there a way to revert that ?


    thanks for your help

  4. #4
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    I just tried to add a for loop to make it scan from both scanners several times (so, independantly from the timer and the rest of the program basically) :


    Code:
        Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
         For k = 1 To 3
                    Dim j As String = 1
                    For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
        [...]
                  Next i
                Next k
                DeviceManager1 = Nothing


    That showed that the first occurence of the loop works (scans once from each scanner) but that's it, the scanners never scan the second time and start blinking, so basically exactly the same problem.
    I also tried to include the Devicemanager declaration in the new loop :


    Code:
         For k = 1 To 3 
        Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
                    Dim j As String = 1
                    For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
        [...]
                  Next i
                DeviceManager1 = Nothing
                Next k
    but it did not change anything.










    The next thing I did wat to log the events within the loop so that I can know where exactly things stop :
    Code:
    Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
        Dim j As String = 1
    
    
        For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
            If DeviceManager1.DeviceInfos(i).Type = 1 Then  'Select only scanners, not webcams etc...
    
                'startpoint to calculate how long it is to scan
                Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
    
                'Directory + file
                Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                Form2.CurrentActionLabel.Text = "Scanning from scanner #" & j
    
    
                Dim Scanner As WIA.Device = DeviceManager1.DeviceInfos(i).connect
    
    
                If IsNothing(Scanner) Then
                    Log(Me.logfilename, Now & " | Scanner #" & j & " not found")
                Else
                    Try
                        Dim Img As WIA.ImageFile
    
                        'log
                        Log(Me.logfilename, Now & " | Scanner #" & j & " | Img initialized")
    
                        With Scanner.Items(1)
                            .Properties("6146").Value = colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
                            .Properties("6147").Value = dpi  'dots per inch/horizontal
                            .Properties("6148").Value = dpi 'dots per inch/vertical
                            .Properties("6149").Value = 0 'x point where to start scan
                            .Properties("6150").Value = 0 'y-point where to start scan
    
                            'Following is A4 paper size. (Not 100% accurate because real A4 Ht errors)
                            .Properties("6151").Value = horizextent 'horizontal exent DPI x inches wide
                            .Properties("6152").Value = vertextent 'vertical extent DPI x inches tall
                            '  .Properties("4104").Value = 8 'bits per pixel
    
                        End With
    
                        'log
                        Log(Me.logfilename, Now & " | Scanner #" & j & " | properties initialized")
    
                        'transfer image
                        Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.
    
                        'log
                        Log(Me.logfilename, Now & " | Scanner #" & j & " |Transfer done")
    
                        'kill previous file if exists to avoid errors
                        If System.IO.File.Exists(targetdir) = True Then
                            Kill(targetdir)
                            'log
                            Log(Me.logfilename, Now & " | Scanner #" & j & " | deleted existing " & targetdir)
    
                        End If
    
                        Img.SaveFile(targetdir)
                        'log
                        Log(Me.logfilename, Now & " | Scanner #" & j & " | saved " & targetdir)
    
                        'last scan
                        Form2.LastFileLabel.Text = "\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                        Form2.LastScanLabel.Text = Now
    
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    Finally
    
                        Scanner = Nothing
                    End Try
                End If
    
                'End time for the scan
                Dim ScanEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
                'log
                Log(Me.logfilename, Now & " | Scanner #" & j & " | Scanned " & targetdir & " | duration: " & (ScanEnd - ScanStart))
    
                j = j + 1
    
            End If
    
        Next i
    and here is the logfile generated :
    Code:
        Scan starts 29/11/2012 9:24:31 AM | Interval :Start scanning with 5 min | Res:100 DPI | 
        29/11/2012 9:24:31 AM | Scanner #1 | Img initialized
        29/11/2012 9:24:31 AM | Scanner #1 | properties initialized
        29/11/2012 9:24:49 AM | Scanner #1 |Transfer done
        29/11/2012 9:24:49 AM | Scanner #1 | saved C:\__2\scans\Scanner1\S1_img_1.TIF
        29/11/2012 9:24:49 AM | Scanner #1 | Scanned C:\__2\scans\Scanner1\S1_img_1.TIF | duration: 18
        29/11/2012 9:24:49 AM | Scanner #2 | Img initialized
        29/11/2012 9:24:49 AM | Scanner #2 | properties initialized
        29/11/2012 9:25:08 AM | Scanner #2 |Transfer done
        29/11/2012 9:25:08 AM | Scanner #2 | saved C:\__2\scans\Scanner2\S2_img_1.TIF
        29/11/2012 9:25:08 AM | Scanner #2 | Scanned C:\__2\scans\Scanner2\S2_img_1.TIF | duration: 19
        29/11/2012 9:25:08 AM | Scanner #1 | Img initialized
        29/11/2012 9:25:08 AM | Scanner #1 | properties initialized
    it appears that things go wrong at this line :

    Img = Scanner.Items(1).Transfer(fileformat) 'scans the image.







    It looks like WIA is happy to switch from scanner 1 to 2 but refuses to come back to scanner 1 for the next round. also, I should precise, when the second scan is supposed to occur, scanner #2 blinks (and not 1 which surprises me).
    Is it possible that scanner#2 is selected as "default scanner" or something like that and if so, is there a way to revert that ?


    thanks for your help

  5. #5
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Trouble automating scanning using WIA and VB.NET

    Looking at what you've put up so far, all i can suggest is have you tried to close the device manager when done, and reopen it for the next scan..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  6. #6
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    Is there any other to "close" the scanner than just setting :
    scanner=nothing
    devicemanager1=nothing

    ?

  7. #7
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Trouble automating scanning using WIA and VB.NET

    MSDN is a little scarce on full details but try either
    Code:
    scanner.close
    scanner.disconnect
    scanner.dispose
    The main problem is that you've opened the first scanner object with one reference of Dim Scanner As WIA.Device = DeviceManager1.DeviceInfos(i).connect this has created a linked object to the scanner, you are then creating a new reference to the Second scanner object using the same Object name without properly disposing of the first..

    WARNING OOP basics discussion bellow - Look up OOP(Object orientated programming) if you do not understand.

    On the second Dim of the Scanner variable, it will now hold reference to a new object (in different memory space) linked to the second scanner with out having properly disconnected the original object linked to the first scanner. This however will not be too much of a problem if you do not need the first scanner again, which actually you do!

    So now when you try to Dim the Scanner Variable a third time to connect to Scanner 1 .. Scanner 1 is already connected to the first instance of the Scanner Object, and will fail..

    Remember that the Scanner Variable only holds a pointer to the object memory location.

    Now another option is to keep reference to both objects in separate variables and switch as needed. This involves a little more code but also will lock out both scanners from any other person trying to use them, and if the scanner is reset or some other disconnection occurs, you will no longer be able to scan (at least until the app is shut down and restarted) ..

    First build the list of devices
    Code:
    Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
        Dim counter as integer = 0
        Dim Lists() As WIA.Device
    
    
        For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
            If DeviceManager1.DeviceInfos(i).Type = 1 Then  'Select only scanners, not webcams etc...
    
                counter +=1
                Redim presurve lists(counter)
                lists(counter) = DeviceManager1.DeviceInfos(i).connect
            End if
        Next i
    now in your scanning loop you select the device as needed
    Code:
        Dim Scanner As WIA.Device
        For i = 1 To counter  'loop through list
    
                'startpoint to calculate how long it is to scan
                Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
    
                'Directory + file
                Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & j & "\S" & j & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                Form2.CurrentActionLabel.Text = "Scanning from scanner #" & j
    
    
                Scanner = lists(i)
    
    
                If IsNothing(Scanner) Then
                    Log(Me.logfilename, Now & " | Scanner #" & j & " not found")
                Else
                    Try
                        Dim Img As WIA.ImageFile

    Hope this helps...
    PS. there are still other short comings but we will address those later...
    Last edited by GremlinSA; November 30th, 2012 at 02:17 AM. Reason: fixed code
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  8. #8
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    Thanks for your reply.
    This was a hell of a lot clearer than a lot of oop tutorial / newbie guides I've read.
    I can't really try out your suggestion until monday (I actually can do it from home but only have one scanner so I won't be able to see if it solves the problem.)
    I am however looking forward to trying it !
    Thanks a lot again and don't hesitate to post your other comments.
    I'll let you know the results

  9. #9
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    Although I couldn't try on the computer with 2 scanners, I tried on my home compter which has only one.

    I can say already that none of these work (they all generate an error message) :
    Code:
    scanner.close
    scanner.disconnect
    scanner.dispose
    I also tried with
    Code:
    System.Runtime.InteropServices.Marshal.ReleaseComObject(Scanner)
    which I found on some tutorial. This does not generate any error but it doesn't solve the problem.



    Then I tried your solution by declaring the connections (I called ScannerCo:
    Code:
    Public Sub InitiateScannersConnections()
    
            ScannerCounter = 0
    
            For i = 1 To DeviceManager1.DeviceInfos.Count
    
                ScannerCounter = ScannerCounter + 1
    
                If DeviceManager1.DeviceInfos(i).Type = 1 Then  'Select only scanners  and not webcams etc...
    
                    ReDim Preserve Me.scanners(0 To ScannerCounter)
                    Me.scanners(ScannerCounter) = DeviceManager1.DeviceInfos(i).DeviceID  'save scanner's ID
    
                    ReDim Preserve Me.ScannerCo(0 To ScannerCounter)
                    Me.ScannerCo(ScannerCounter) = DeviceManager1.Deviceinfos(i).Connect
    
                End If
            Next i
    
        End Sub
    and then call this connection in the scannerloop sub:

    Code:
            For i = 1 To ScannerCounter
    
    
                'Directory + file
                Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & i & "\S" & i & "_" & Me.FilePrefix.Text & Me.CurrFileIndex & "." & Me.FileExt.SelectedItem
                Form2.CurrentActionLabel.Text = "Scanning from scanner #" & i
    
                Scanner = ScannerCo(i)
    ...
    This actually doesn't work even with 1 scanner (i'll try tomorrow if it works with more than one)
    The first scan occurs normally, but for the second scan, I get this error:
    Code:
    Value does not fall within the expected range
    at this line :
    Code:
    Dim Img As WIA.ImageFile = Scanner.Items(1).Transfer(fileformat) 'scans the image.
    (FYI: at that moment, fileformat = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}")
    I should precise that if I revert back to the "old" method, ie : Scanner = DeviceManager1.deviceinfos(i).connect instead of Scanner = ScannerCo(i), I don't have this error.
    Last edited by vince8290; December 2nd, 2012 at 12:20 AM.

  10. #10
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    @ GremlinSA
    I tried your method of declaring the connections upfront and calling them back instead of creating a new each time.
    I Finally managed to get it to work with one scanner quite easily.
    But it still doesn't work with more than 1 scanner. I tried many many different things but the very best result I could get was the first scan of the first scanner succeeds. Even during the first occurence of the loop, the second scanner doesn't scan.

    I'm hopeless.

    I can't believe there's nothing in WIA to unlock or disconnect a scanner object !

    I guess if I don't find a way to make that work, I may have to try using TWAIN instead of WIA ?
    Does anyone have a newbie freindly tutorial or source to drive scanners using twain in VB?

  11. #11
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: Trouble automating scanning using WIA and VB.NET

    Okay ... Lets take this further...

    Instead of passing objects back and forth and making new ones for every loop, lets go the more direct route.. Declare all variables up front, and use the declared ones..

    Code:
        Private Sub test()
            Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
            Dim counter As Integer = 0
            Dim Scanners() As WIA.Device
    
    
            For i = 1 To DeviceManager1.DeviceInfos.Count 'loop through all devices
                If DeviceManager1.DeviceInfos(i).Type = 1 Then  'Select only scanners, not webcams etc...
    
                    counter += 1
                    ReDim Preserve Scanners(counter)
                    Scanners(counter) = DeviceManager1.DeviceInfos(i).connect
                    ' set scanner properties here... 
                    'only needs to be set once....
                    With Scanners(counter)
                        .Properties("6146").Value = colorcode '4 is Black-white,gray is 2, color 1 (Color Intent)
                        .Properties("6147").Value = dpi  'dots per inch/horizontal
                        .Properties("6148").Value = dpi 'dots per inch/vertical
                        .Properties("6149").Value = 0 'x point where to start scan
                        .Properties("6150").Value = 0 'y-point where to start scan
    
                        'Following is A4 paper size. (Not 100% accurate because real A4 Ht errors)
                        .Properties("6151").Value = horizextent 'horizontal exent DPI x inches wide
                        .Properties("6152").Value = vertextent 'vertical extent DPI x inches tall
                        '  .Properties("4104").Value = 8 'bits per pixel
                    End With
    
                End If
            Next i
            Dim DeviceManager1 = CreateObject("WIA.DeviceManager")   'wia device manager
    
            Dim Img As WIA.ImageFile
    
            For j = 1 To 5 'loop through all devices 5 times...
    
                For i = 1 To counter 'loop through all found devices
    
                    'startpoint to calculate how long it is to scan
                    Dim ScanStart = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
    
                    'Directory + file
                    Dim targetdir = Me.ProjectFolderBox.Text & "\scans\Scanner" & i & "\S" & i & "_" & Me.FilePrefix.Text & j & "." & Me.FileExt.SelectedItem
                    Form2.CurrentActionLabel.Text = "Scanning from scanner #" & i
    
                    If IsNothing(Scanners(counter)) Then
                        Log(Me.logfilename, Now & " | Scanner #" & i & " not found")
                    Else
                        Try
    
    
                            'transfer image
                            Img = Scanners(counter).Items(1).Transfer(fileformat) 'scans the image.
    
                            'kill previous file if exists to avoid errors
                            If System.IO.File.Exists(targetdir) = True Then
                                Kill(targetdir)
                            End If
                            If img IsNot Nothing Then 'check if we got a image first..
                                Img.SaveFile(targetdir)
    
                                'last scan
                                Form2.LastFileLabel.Text = "\Scanner" & 1 & "\S" & i & "_" & Me.FilePrefix.Text & j & "." & Me.FileExt.SelectedItem
                                Form2.LastScanLabel.Text = Now
                            Else
                                ' log that no img was received here .....
    
                            End If
    
                        Catch ex As Exception
                            MsgBox(ex.Message)
                        Finally
                            ' clear the image ---- Might not be needed
                            ' include if the first scanned image is repeated
    
                            'img = Nothing
                        End Try
                    End If
    
                    'End time for the scan
                    Dim ScanEnd = DateAndTime.Second(Now) + (DateAndTime.Minute(Now) * 60) + (DateAndTime.Hour(Now) * 3600)
    
                    'log
                    Log(Me.logfilename, Now & " | Scanner #" & i & " | Scanned " & targetdir & " | duration: " & (ScanEnd - ScanStart))
    
    
                Next i
    
            Next j
    
        End Sub
    I've also rearranged some of the code , to cover some of the other issues...

    Call the sub ONCE.. and it should scan 5 times from each scanner..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  12. #12
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Trouble automating scanning using WIA and VB.NET

    I'd suspect a timing issue. Try waiting a few minutes between scans.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    @ David.

    I suspected that too when I finally managed to get the scanners to scan 2 times in a row ! (big victory for me)
    so far, I had tried every minute or every 2 minutes (given that each scan lasted only 15sec I thought it would be enough).
    When I tried 6 minutes apart, I managed to get 2 scans in a row but never got the third one.
    Therefore, I decided to try every 8 minutes and every 10 minutes thinking that I had found the solution but it didn't change anything :
    2 scans succeeded, but I never got the 3rd one, odd...
    Before leaving the office today, I started 1 scan every hour, we'll see tomorrow morning the result !


    @ GremlinSA
    Thanks so much for the time you spend on my problem.
    I will incorporate your corrected code tomorrow and see how it goes.
    Last edited by vince8290; December 4th, 2012 at 02:37 AM.

  14. #14
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    Quick update :

    @David
    Scanning every 60minutes is not different from scanning every 6 minutes. here is the log file :
    Scan starts 4/12/2012 4:50:44 PM | Interval :Start scanning with 60 min | Res:400 DPI |
    4/12/2012 4:50:44 PM | Scanner #1 | Connected to scanner 1
    4/12/2012 4:50:44 PM | Scanner #1 | Img initialized
    4/12/2012 4:50:44 PM | Scanner #1 | properties initialized
    4/12/2012 4:51:29 PM | Scanner #1 | Transfer done
    4/12/2012 4:51:29 PM | Scanner #1 | saved D:\_60min\scans\Scanner1\S1_img_1.TIF
    4/12/2012 4:51:30 PM | Scanner #2 | Connected to scanner 2
    4/12/2012 4:51:30 PM | Scanner #2 | Img initialized
    4/12/2012 4:51:30 PM | Scanner #2 | properties initialized
    4/12/2012 4:52:15 PM | Scanner #2 | Transfer done
    4/12/2012 4:52:15 PM | Scanner #2 | saved D:\_60min\scans\Scanner2\S2_img_1.TIF
    4/12/2012 5:50:49 PM | Scanner #1 | Connected to scanner 1
    4/12/2012 5:50:49 PM | Scanner #1 | Img initialized
    4/12/2012 5:50:49 PM | Scanner #1 | properties initialized
    4/12/2012 5:51:34 PM | Scanner #1 | Transfer done
    4/12/2012 5:51:34 PM | Scanner #1 | saved D:\_60min\scans\Scanner1\S1_img_2.TIF
    4/12/2012 5:51:39 PM | Scanner #2 | Connected to scanner 2
    4/12/2012 5:51:39 PM | Scanner #2 | Img initialized
    4/12/2012 5:51:39 PM | Scanner #2 | properties initialized
    4/12/2012 5:52:23 PM | Scanner #2 | Transfer done
    4/12/2012 5:52:23 PM | Scanner #2 | saved D:\_60min\scans\Scanner2\S2_img_2.TIF
    4/12/2012 6:50:45 PM | Scanner #1 | Connected to scanner 1
    4/12/2012 6:50:45 PM | Scanner #1 | Img initialized
    4/12/2012 6:50:45 PM | Scanner #1 | properties initialized


    and then the scanner flashed all night long...



    @GremlinSA
    I pasted your code and used it as is (except two correction :
    forgot the .items(1) at this line With Scanners(counter)
    and replaced the "Scanners(counter)" indices by "Scanner(i)" inside the For i = 1 To counter loop(it was always scanning from scanner 2).

    It's not working. not even the first scan. and scanner #2 flashes.



    by the way, once these scanners start flashing, the only way I have to "unblock" it, is to unplug it.
    is there a way to turn off the scanner and back on using WIA ?

  15. #15
    Join Date
    Nov 2012
    Posts
    10

    Re: Trouble automating scanning using WIA and VB.NET

    As a reminder, i'm a newbie in the field but, by reading this, I wondered if creating/managing several threads would help:
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    I have no idea if it would help, nor how to implement that, but I'm desperately looking for solutions and I thought some experts from the forum may be able to help on that.
    what do you think ?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center