How do I scan?

MarMan

Member
Joined
Jan 4, 2010
Messages
17
Programming Experience
10+
Hello.

I am trying to scan images in VB.NET. I searched this site for almost an hour and found questions, but little in the way of answers, but some redirection which did not resolve the issue.

I downloaded the example here openTwain which has 7 projects doing a whole bunch of things. I do not have the scanner attached to my PC. Nor can I run any of these examples which do way more than I am trying to do.

Then I went here CodeProject: .NET TWAIN image scanner. Free source code and programming help, found the VB version on planetsourcecode, but it will not run. I tried to duplicate what it does, but I couldn't get either of these line to be recognized:
VB.NET:
Imports TwainGui.vb.TwainLib
Imports TwainGUI.vb.GdiPlusLib
I am a VB.NET novice, but a VB veteran since VB3. I understand that the differences between .NET and VB are huge, but what I fail to understand is why no one has been able to post an example of how to scan? My company is not willing to purchase any 3rd party tools and my boss believes it can be done through the api, which some posts seem to imply but fail to show how.

Can anyone please help me?
 
To answer your questions:

1) The scanner is attached to the PC of the person responsible for this task. I can not be seen on the network.

2) I only choose twain because I thought it would be easiest. That's what the current app uses. I will see what I can find on WIA.

Thanks
 
Thanks for the replies. I found a free dll that uses twain that I haven't seen posted anywhere on this site: Dosadi: EZTwain Classic. Its hard to beat free, and it requires no installation apart from putting the dll in your app directory (or system32). The ease of installation makes it the preferable choice over WIA.

I declared it like this:
VB.NET:
    Private Declare Function AcquireNative Lib "eztw32.dll" Alias "TWAIN_AcquireNative" ( _
       ByVal hwndApp As Integer, _
       ByVal wPixTypes As Integer) As Integer

    Private Declare Function AcquireToFilename Lib "eztw32.dll" Alias "TWAIN_WriteNativeToFilename" ( _
       ByVal hwndApp As Integer, _
       ByVal sFile As String) As Integer

    Private Declare Function AcquireToClipboard Lib "eztw32.dll" Alias "TWAIN_AcquireToClipboard" ( _
        ByVal hwndApp As Integer, _
        ByVal wPixTypes As Integer) As Integer

If I can get this to scan multiple pages I will be all set. I would also like to remove the dialog. I will post any solutions here that I find. Does anyone have any knowledge using this dll or know of a link?

If there isn't a way to scan multiple pages with this free dll then I will use WIA.

Thanks again for the help.
 
Thanks for your assistance Matt. Looks like I may be stuck with WIA. But a working COM is better than a not working DLL. But if anyone needs to scan a single page, eztwain works well. Requires no extra steps for the setup as long as the DLL is placed where the app can find it. Here's the code:
VB.NET:
        Private Declare Sub FreeNative Lib "eztw32.dll" Alias "TWAIN_FreeNative" ( _
             ByVal hdib As Integer)

        intHandle = AcquireNative(Me.Handle, 0)
        rc = AcquireToFilename(intHandle, AppPath() & "Scan.bmp")
        FreeNative(intHandle)

If I get WIA to work I will post that for others to use.
 
Thanks Matt. I found that earlier, but it loaded with several errors. I tried it today and was able to open it fine. Maybe they updated it or I did something, who knows. But I'm glad you pointed me to it again, I will post some code if I have any luck.:)
 
Hello. I have have been working on using TWAIN from VB, I used that code that you pointed me to Matt as an example but have hit a wall. When I try to get the capabilities of the scanner I get 18874403 for the physical height and width (which should not be the same). i don't think it could be a case of signed or unsigned because an integer is -2147483648 through +2147483647 and a short is -32768 through 32767. Neither of those add up.

Here is the c structure:
VB.NET:
 typedef unsigned short TW_UINT16;

typedef struct {
   TW_UINT16  Cap; /* id of capability to set or get, e.g. CAP_BRIGHTNESS */
   TW_UINT16  ConType; /* TWON_ONEVALUE, _RANGE, _ENUMERATION or _ARRAY   */
   TW_HANDLE  hContainer; /* Handle to container of type Dat              */
} TW_CAPABILITY, FAR * pTW_CAPABILITY;

And my VB.NET equivalent:
VB.NET:
     <StructLayout(LayoutKind.Sequential, Pack:=2)> Private Structure twsCapability
        Public Cap As twCap                             'Short
        Public ConType As twContainerTyoe        'Short
        Public Handle As IntPtr     'Pointer to a container
    End Structure

And the code that calls it:
VB.NET:
   <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DS_Capability( _
        <[In](), Out()> ByVal pOrigin As twsIdentity, _
        <[In]()> ByVal pSource As twsIdentity, _
        ByVal dg As twDG, _
        ByVal dat As twDAT, _
        ByVal msg As twMSG, _
        <[In](), Out()> ByRef pData As twsCapability) As twRC
    End Function

            twCapability = New twsCapability
            With twCapability
                .Cap = twCap.IPhysicalHeight 'twCap.IPhysicalWidth
                '.ConType = 7 'TWON_DONTCARE16
                '.Handle = Nothing
            End With
            rc = DS_Capability(mtwApp, mtwSource, twDG.Control, twDAT.Capability, twMSG.Get, twCapability)
            If rc = twRC.Success Then
                Select Case twCapability.ConType
                    Case twContainerTyoe.One
                        twOneValue = New TW_ONEVALUE
                        Marshal.PtrToStructure(twCapability.Handle, twOneValue)
                        strItems = CType(twOneValue.Item, String)
                End Select
                Marshal.FreeHGlobal(twCapability.Handle)
            End If

Something does not match up, anyone have any ideas?
 
I am starting to think that it has something to do with marshaling data from a pointer to memory allocated by unmanaged code. Does anyone know the proper way to do this? Is my method above correct or incorrect?
 
I found out that a pointer to a structure is not the same as a pointer to the memory that the structure occupies. Changed my code to copy the members explicitly. I didn't post it because it appears that no one on this forum is interested. If anyone asks, I will post it.
 
Back
Top