Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2940
  • Last Modified:

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
0
Simon482
Asked:
Simon482
  • 2
1 Solution
 
lluddenCommented:
I started out with the EZTwain, but after having some difficulties, I went with a fairly cost effective commercial package - Imageman.Net Twain.  It costs $325 for a single developer and is pretty easy to use.

0
 
Simon482Author Commented:
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
0
 
Simon482Author Commented:
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.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now