Link to home
Start Free TrialLog in
Avatar of Simon482
Simon482Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Scanning in VB.Net using TWAIN or WIA

Experts,

I am trying to add scanning functionality to my WPF application,

The application is for managing invoices received; each time we receive an invoice from an Agent we log it into this system.

Previously I gave the user the ability to link the record to a pre-scanned pdf of the invoice, but this is proving to onerous for the user and they want to be able to scan the invoice in as they are logging it into the system. The idea is that as they log the invoice, when they click 'OK' they are prompted to scan a file and this is saved to an automatic location.

Trying to implement this I have been scouring the internet for quite some time and have picked up code examples for TWAIN and WIA.

I Started with EZTwain and added the following to my project:

 
Dim mainWindowPtr As Integer = New WindowInteropHelper(Me).Handle
            EZTwain.SelectImageSource(mainWindowPtr)
            intCompletionCode = EZTwain.AcquireToFilename(0, FileNameAndPath)
            Select Case intCompletionCode
                Case 0
                    MsgBox(" Success !!! ")
                Case -1
                    MsgBox("The Acquire failed")
                Case -2
                    MsgBox("File Open Error (Invalid path or name, or access denied)")
                Case -3
                    MsgBox("Invalid DIB, or image incompatible with file format, or ...")
                Case -4
                    MsgBox("Writing Failed, possibly output device is full.")
                Case -10
                    MsgBox("User cancelled File Save Dialog")
                Case Else
                    MsgBox("Unknown Error: " & intCompletionCode)
            End Select

Open in new window


Which worked exactly how I needed it to while in debug mode, but us soon as I deployed the project the compiled version brings up an error message:

"Failed to enable Data Source.
DataSource Operation Failed
RC: TWCR_FAILURE
CC: TWCC_SUCCESS"

and AcquireToFilename returned code -1, meaning The Acquire Failed.

As the error message mentioned the failure to enable a datasource, I amended the code using another example, as follows:

 
Dim mainWindowPtr As Integer = New WindowInteropHelper(Me).Handle
            EZTwain.SelectImageSource(mainWindowPtr)
            EZTwain.SetHideUI(1)
            EZTwain.LoadSourceManager()
            EZTwain.OpenSourceManager(mainWindowPtr)
            EZTwain.OpenDefaultSource()
            EZTwain.SetCurrentResolution(300)
            EZTwain.EnableSource(mainWindowPtr)
            intCompletionCode = EZTwain.AcquireToFilename(0, FileNameAndPath)
            EZTwain.DisableSource()
            EZTwain.CloseSource()
            EZTwain.CloseSourceManager(mainWindowPtr)
            EZTwain.UnloadSourceManager()
            Select Case intCompletionCode
                Case 0
                    MsgBox(" Success !!! ")
                Case -1
                    MsgBox("The Acquire failed")
                Case -2
                    MsgBox("File Open Error (Invalid path or name, or access denied)")
                Case -3
                    MsgBox("Invalid DIB, or image incompatible with file format, or ...")
                Case -4
                    MsgBox("Writing Failed, possibly output device is full.")
                Case -10
                    MsgBox("User cancelled File Save Dialog")
                Case Else
                    MsgBox("Unknown Error: " & intCompletionCode)
            End Select

Open in new window


This version hides the GUI, but works on the same principal and went through all the motions as expected, except when it finished scanning I get the following errors:

"Attempt to set capability outside State 4"; and

"Error setting Native transfer mode
[no details available]"


No image was saved.

I gave up on EZTwain for a bit and tried stealing from this project at PlanetSourceCode.

But, having trouble getting my head around this, I did a bit more Googling and read some opinions that TWAIN is out and WIA is in

Looking fo some decent WIA examples I found one here, but it is in C#, a language I have no knowlege or interest in.

I tried to convert it all the same and came up with the following:

 
Dim wiaItem As WIALib.Item
        Dim selectUsingUI As New Object
        selectUsingUI = System.Reflection.Missing.Value
        Dim wiaManager As New WIALib.Wia
        Dim WiaRoot As WIALib.Item = wiaManager.Create(selectUsingUI)
        Dim wiaPics As WIALib.Collection = WiaRoot.GetItemsFromUI(WIALib.WiaFlag.UseCommonUI, WIALib.WiaIntent.ImageTypeColor)
        For Each wiaObj As Object In wiaPics
            wiaItem = Marshal.CreateWrapperOfType(wiaObj, GetType(WIALib.Item))
            Dim imageFileName = Path.GetTempFileName()
            wiaItem.Transfer(imageFileName, False)
        Next

Open in new window


Which is clearly wrong. I get my scan window, but I am using 'Marshall.CreateWrapperOfType' incorrectly as I am getting the exception:

"The type must be __ComObject or be derived from __ComObject."

I tried using WIALib.ItemClass, instead of WIALib.Item, but that just leads to another error:

"Reference to class 'ItemClass' is not allowed when its assembly is linked using No-PIA mode."


I have now spent far too long on this and am resorting to asking for help, so if anyone tell me how I can implement scanning functionality using either technology then I would be very grateful.

Many thanks

Simon
ASKER CERTIFIED SOLUTION
Avatar of lludden
lludden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Simon482

ASKER

Thank you both for the input,

I have spoken to the bossman and he is not adverse to spending 325 notes, if that is what it takes (though the $3,000 ones are a maybe a bit too dear).

I am having a go on the trial version of Imageman.Net Twain and if it works well then we'll probably go with that.

In the meantime, however, if anyone can see anything obviously wrong with my code and knows how i can get it working for free, then that would be useful.

Cheers,

Simon
Whilst throwing money at a problem is not my favoured solution, it does seem to work. This product is easy to use and more than meets our needs.

Thanks.