Command line twain

superbem

Member
Joined
May 17, 2007
Messages
22
Programming Experience
10+
I'm trying to make a command line twain for a scanner take the image and save it with the configurations I want, but I'm in dificulties with the basic.
I'm trying to put all the commands in the load form of the following code (I think it have all functions related to twain_32.dll).

Is someone interested to help me finish this?

HTML:
Imports System
Imports System.Collections
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Text
Imports System.Drawing
Imports System.ComponentModel

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Dim d As New TwainLib.Twain

        'Dim d As New System.IntPtr
        'd = 21

        Dim b As New TwainLib.Twain
        b.Init(Handle)
        b.Select()
        'b.SelectDefault()
        b.Acquire()
        'b.Acquire(TwainLib.TwSS.TwSS_A4, , , TwainLib.TwColourType.twBW)
        ' b.TransferPictures()
        Dim a As New TwainLib.TwainCommand
        a = TwainLib.TwainCommand.TransferReady
        MsgBox(a)
        'aaa.Acquire()

        'aaa.TransferPictures()
        Dim a1 As New TwainLib.no
        'a1 = 1
        MsgBox(a1.ToString)
        b.Dispose()
        ' d.Dispose()
    End Sub
End Class

Namespace TwainLib

    Public Enum TwainCommand
        [Not] = -1
        Null = 0
        TransferReady = 1
        CloseRequest = 2
        CloseOk = 3
        DeviceEvent = 4
        Failure = 5
    End Enum

    Public Class Twain

        Private hwnd As IntPtr
        Private appid As no
        Private srcds As no
        Private evtmsg As TwEvent
        Private winmsg_m As WINMSG_S

        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMparent(<[In](), Out()> ByVal origin As no, ByVal zeroptr As IntPtr, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByRef refptr As IntPtr) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMident(<[In](), Out()> ByVal origin As no, ByVal zeroptr As IntPtr, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal idds As no) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMstatus(<[In](), Out()> ByVal origin As no, ByVal zeroptr As IntPtr, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal dsmstat As TwStatus) As TwRC
        End Function

        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSuserif(<[In](), Out()> ByVal origin As no, <[In](), Out()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByVal guif As TwUserInterface) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSevent(<[In](), Out()> ByVal origin As no, <[In](), Out()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByRef evt As TwEvent) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSstatus(<[In](), Out()> ByVal origin As no, <[In]()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal dsmstat As TwStatus) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DScap(<[In](), Out()> ByVal origin As no, <[In]()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal capa As TwCapability) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSiinf(<[In](), Out()> ByVal origin As no, <[In]()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal imginf As TwImageInfo) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSixfer(<[In](), Out()> ByVal origin As no, <[In]()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, ByRef hbitmap As IntPtr) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSpxfer(<[In](), Out()> ByVal origin As no, <[In]()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal pxfr As TwPendingXfers) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSilayout(<[In](), Out()> ByVal origin As no, <[In](), Out()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal imglo As TwImageLayout) As TwRC
        End Function
        <DllImport("twain_32.dll", EntryPoint:="#1")> Private Shared Function DSMlayout(<[In](), Out()> ByVal origin As no, <[In](), Out()> ByVal dest As no, ByVal dg As TwDG, ByVal dat As TwDAT, ByVal msg As TwMSG, <[In](), Out()> ByVal imglo As IntPtr) As TwRC
        End Function


        <DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalAlloc(ByVal flags As Integer, ByVal size As Integer) As IntPtr
        End Function
        <DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalLock(ByVal handle As IntPtr) As IntPtr
        End Function
        <DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalUnlock(ByVal handle As IntPtr) As Boolean
        End Function
        <DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalFree(ByVal handle As IntPtr) As IntPtr
        End Function

        <DllImport("user32.dll", ExactSpelling:=True)> Private Shared Function GetMessagePos() As Integer
        End Function
        <DllImport("user32.dll", ExactSpelling:=True)> Private Shared Function GetMessageTime() As Integer
        End Function

        <DllImport("gdi32.dll", ExactSpelling:=True)> Private Shared Function GetDeviceCaps(ByVal hDC As IntPtr, ByVal nIndex As Integer) As Integer
        End Function
        <DllImport("gdi32.dll", CharSet:=CharSet.Auto)> Private Shared Function CreateDC(ByVal szdriver As String, ByVal szdevice As String, ByVal szoutput As String, ByVal devmode As IntPtr) As IntPtr
        End Function
        <DllImport("gdi32.dll", ExactSpelling:=True)> Private Shared Function DeleteDC(ByVal hdc As IntPtr) As Boolean
        End Function

        Private Const CountryUSA As Short = 1
        Private Const LanguageUSA As Short = 13

        Public Sub New()
            appid = New no
            appid.Id = IntPtr.Zero
            appid.Version.MajorNum = 1
            appid.Version.MinorNum = 1
            appid.Version.Language = LanguageUSA
            appid.Version.Country = CountryUSA
            appid.Version.Info = "TWAIN Scanner"
            appid.ProtocolMajor = TwProtocol.Major
            appid.ProtocolMinor = TwProtocol.Minor
            appid.SupportedGroups = CType(TwDG.Image Or TwDG.Control, Integer)
            appid.Manufacturer = "Kod Efisien Sdn Bhd"
            appid.ProductFamily = "TWAIN 32 Scanner"
            appid.ProductName = "ScanSoft"

            srcds = New no
            srcds.Id = IntPtr.Zero

            evtmsg.EventPtr = Marshal.AllocHGlobal(Marshal.SizeOf(winmsg_m))
        End Sub

        Public Sub Dispose()
            Marshal.FreeHGlobal(evtmsg.EventPtr)
        End Sub

        Protected Overrides Sub Finalize()
            Marshal.FreeHGlobal(evtmsg.EventPtr)
        End Sub

        Public Sub Init(ByVal hwndp As IntPtr)
            Finish()
            Dim rc As TwRC = DSMparent(appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.OpenDSM, hwndp)
            If (rc = TwRC.Success) Then
                rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.no, TwMSG.GetDefault, srcds)
                If (rc = TwRC.Success) Then
                    hwnd = hwndp
                Else
                    rc = DSMparent(appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.CloseDSM, hwndp)
                End If
            End If
        End Sub

        Public Sub [Select](Optional ByRef Model As String = "")
            Dim rc As TwRC
            CloseSrc()
            If Equals(appid.Id, IntPtr.Zero) = True Then
                Init(hwnd)
                If Equals(appid.Id, IntPtr.Zero) = True Then
                    Return
                End If
            End If
            rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.no, TwMSG.UserSelect, srcds)
            Model = srcds.ProductName.ToString
        End Sub

        Public Sub [SelectDefault](Optional ByRef Model As String = "")
            Dim rc As TwRC
            CloseSrc()
            If Equals(appid.Id, IntPtr.Zero) = True Then
                Init(hwnd)
                If Equals(appid.Id, IntPtr.Zero) = True Then
                    Return
                End If
            End If
            rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.no, TwMSG.GetDefault, srcds)
            Model = srcds.ProductName.ToString
        End Sub

        Public Sub Acquire(Optional ByVal PaperSize As TwSS = TwSS.TwSS_A4, Optional ByVal XScale As Single = 1, _
        Optional ByVal YScale As Single = 1, Optional ByVal SColor As TwColourType = TwColourType.twRGB, _
        Optional ByVal TopMargin As Single = 0.0, Optional ByVal LeftMargin As Single = 0.0, _
        Optional ByVal RightMargin As Single = 0.0, Optional ByVal BottomMargin As Single = 0.0, _
        Optional ByVal FrameNumber As Integer = 1, Optional ByVal PageNumber As Integer = 1, _
        Optional ByVal DocumentNumber As Integer = 1)

            Dim rc As TwRC
            CloseSrc()
            If Equals(appid.Id, IntPtr.Zero) = True Then
                Init(hwnd)
                If Equals(appid.Id, IntPtr.Zero) = True Then
                    Return
                End If
            End If

            rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.no, TwMSG.OpenDS, srcds)
            If (rc <> TwRC.Success) Then
                Return
            End If

            'Set Unit to Pixels
            Dim Pixels As TwUnit = TwUnit.twPIXELS
            Dim capUnit As TwCapability = New TwCapability(TwCap.IUnits, Pixels, TwType.Int16)
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capUnit)
            If rc <> TwRC.Success Then
                CloseSrc()
                Return
            End If

            ''Set X Resolution
            'Dim XResolution As Single = 1
            'Dim capX As TwCapability = New TwCapability(TwCap.XResolution, XResolution, TwType.Fix32)

            'rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capX)
            'If rc <> TwRC.Success Then
            '    CloseSrc()
            '    Return
            'End If

            ''Set Y Resolution
            'Dim capY As TwCapability = New TwCapability(TwCap.YResolution, YResolution, TwType.Fix32)

            'rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, capY)
            'If rc <> TwRC.Success Then
            '    CloseSrc()
            '    Return
            'End If

            Dim xcap As TwCapability = New TwCapability(TwCap.XScaling, XScale, TwType.Fix32)
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, xcap)
            If rc <> TwRC.Success Then
                CloseSrc()
                Return
            End If

            xcap = New TwCapability(TwCap.YScaling, YScale, TwType.Fix32)
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, xcap)
            If rc <> TwRC.Success Then
                CloseSrc()
                Return
            End If


            Dim ps As TwCapability = New TwCapability(TwCap.SupportedSizes, PaperSize, TwType.Int16)
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, ps)
            If rc <> TwRC.Success Then
                CloseSrc()
                Return
            End If

            'Set Layout
            Dim Layout As TwImageLayout = New TwImageLayout
            rc = DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, Layout)
            If rc <> TwRC.Success Then
                CloseSrc()
                Return
            End If
            If TopMargin > 0 Then
                Layout.Frame.Top.FromFloat(CSng(TopMargin))
            End If
            If LeftMargin > 0 Then
                Layout.Frame.Left.FromFloat(CSng(LeftMargin))
            End If
            If RightMargin > 0 Then
                Layout.Frame.Right.FromFloat(CSng(RightMargin))
            End If
            If BottomMargin > 0 Then
                Layout.Frame.Bottom.FromFloat(CSng(BottomMargin))
            End If
            Layout.FrameNumber = 1
            Layout.PageNumber = 1
            Layout.DocumentNumber = 1
            rc = Me.DSilayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Set, Layout)
            If rc <> TwRC.Success Then
                CloseSrc()
                Return
            End If

            'Dim ptr1 As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(Layout))
            'Marshal.StructureToPtr(Layout, ptr1, False)

            'rc = Me.DSMlayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Get, ptr1)
            'If rc <> TwRC.Success Then
            '    CloseSrc()
            '    Return
            'End If
            ''Dim ptr2 As IntPtr = ptr1
            'Marshal.PtrToStructure(ptr1, Layout)
            'If TopMargin > 0 Then
            '    Layout.Frame.Top.FromFloat(CSng(TopMargin))
            'End If
            'If LeftMargin > 0 Then
            '    Layout.Frame.Left.FromFloat(CSng(LeftMargin))
            'End If
            'If RightMargin > 0 Then
            '    Layout.Frame.Right.FromFloat(CSng(RightMargin))
            'End If
            'If BottomMargin > 0 Then
            '    Layout.Frame.Bottom.FromFloat(CSng(BottomMargin))
            'End If
            'Layout.FrameNumber = 1
            'Layout.PageNumber = 1
            'Layout.DocumentNumber = 1
            'Marshal.StructureToPtr(Layout, ptr1, True)

            'rc = Me.DSMlayout(appid, srcds, TwDG.Image, TwDAT.ImageLayout, TwMSG.Set, ptr1)

            'If rc <> TwRC.Success Then
            '    CloseSrc()
            '    Return
            'End If

            'Scan Colour
            Dim Ccap As TwCapability = New TwCapability(TwCap.IPixelType, SColor, TwType.UInt16)
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, Ccap)
            If (rc <> TwRC.Success) Then
                CloseSrc()
                Return
            End If

            Dim cap As TwCapability = New TwCapability(TwCap.XferCount, 1, TwType.Int16)
            rc = DScap(appid, srcds, TwDG.Control, TwDAT.Capability, TwMSG.Set, cap)
            If (rc <> TwRC.Success) Then
                CloseSrc()
                Return
            End If

            Dim guif As TwUserInterface = New TwUserInterface
            guif.ShowUI = 1
            guif.ModalUI = 1
            guif.ParentHand = hwnd
            rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.EnableDS, guif)
            If (rc <> TwRC.Success) Then
                CloseSrc()
                Return
            End If

        End Sub

        Public Function TransferPictures() As ArrayList
            Dim pics As ArrayList = New ArrayList
            If Equals(srcds.Id, IntPtr.Zero) Then
                Return pics
            End If

            Dim rc As TwRC
            Dim hbitmap As IntPtr = IntPtr.Zero
            Dim pxfr As TwPendingXfers = New TwPendingXfers

            Do
                pxfr.Count = 0
                hbitmap = IntPtr.Zero

                Dim iinf As TwImageInfo = New TwImageInfo
                rc = DSiinf(appid, srcds, TwDG.Image, TwDAT.ImageInfo, TwMSG.Get, iinf)
                If (rc <> TwRC.Success) Then
                    CloseSrc()
                    Return pics
                End If

                'StartScan
                rc = DSixfer(appid, srcds, TwDG.Image, TwDAT.ImageNativeXfer, TwMSG.Get, hbitmap)
                If (rc <> TwRC.XferDone) Then
                    CloseSrc()
                    Return pics
                End If

                rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.EndXfer, pxfr)
                If (rc <> TwRC.Success) Then
                    CloseSrc()
                    Return pics
                End If

                pics.Add(hbitmap)
            Loop While (pxfr.Count <> 0)

            rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.Reset, pxfr)
            Return pics
        End Function

        Public Function PassMessage(ByVal m As Message) As TwainCommand
            If Equals(srcds.Id, IntPtr.Zero) Then
                Return TwainCommand.Not
            End If

            Dim pos As Integer = GetMessagePos()

            winmsg_m.hwnd = m.HWnd
            winmsg_m.message = m.Msg
            winmsg_m.wParam = m.WParam
            winmsg_m.lParam = m.LParam
            winmsg_m.time = GetMessageTime()
            winmsg_m.x = pos 'CType(pos, Short)
            winmsg_m.y = Int(pos / 2 ^ 16) 'CType(Int(pos / 2 ^ 16), Short)

            Marshal.StructureToPtr(winmsg_m, evtmsg.EventPtr, False)
            evtmsg.Message = 0

            Dim rc As TwRC = DSevent(appid, srcds, TwDG.Control, TwDAT.Event, TwMSG.ProcessEvent, evtmsg)
            If (rc = TwRC.NotDSEvent) Then
                Return TwainCommand.Not
            End If

            If (rc = TwRC.Failure) Then
                Return TwainCommand.Failure
            End If

            If (evtmsg.Message = CType(TwMSG.XFerReady, Short)) Then
                Return TwainCommand.TransferReady
            End If

            If (evtmsg.Message = CType(TwMSG.CloseDSReq, Short)) Then
                Return TwainCommand.CloseRequest
            End If
            If (evtmsg.Message = CType(TwMSG.CloseDSOK, Short)) Then
                Return TwainCommand.CloseOk
            End If
            If (evtmsg.Message = CType(TwMSG.DeviceEvent, Short)) Then
                Return TwainCommand.DeviceEvent
            End If

            Return TwainCommand.Null
        End Function

        Public Sub CloseSrc()
            Dim rc As TwRC
            If Not Equals(srcds.Id, IntPtr.Zero) Then
                Dim guif As TwUserInterface = New TwUserInterface
                rc = DSuserif(appid, srcds, TwDG.Control, TwDAT.UserInterface, TwMSG.DisableDS, guif)
                rc = DSMident(appid, IntPtr.Zero, TwDG.Control, TwDAT.no, TwMSG.CloseDS, srcds)
            End If
        End Sub

        Public Sub Finish()
            Dim rc As TwRC
            CloseSrc()
            If Not Equals(appid.Id, IntPtr.Zero) Then
                rc = DSMparent(appid, IntPtr.Zero, TwDG.Control, TwDAT.Parent, TwMSG.CloseDSM, hwnd)
            End If
            appid.Id = IntPtr.Zero
        End Sub

        Public Shared ReadOnly Property ScreenBitDepth() As Integer
            Get
                Dim screenDC As IntPtr = CreateDC("DISPLAY", Nothing, Nothing, IntPtr.Zero)
                Dim bitDepth As Integer = GetDeviceCaps(screenDC, 12)
                bitDepth *= GetDeviceCaps(screenDC, 14)
                DeleteDC(screenDC)
                Return bitDepth
            End Get
        End Property

        <StructLayout(LayoutKind.Sequential, Pack:=4)> Friend Structure WINMSG_S
            Public hwnd As IntPtr
            Public message As Integer
            Public wParam As IntPtr
            Public lParam As IntPtr
            Public time As Integer
            Public x As Integer
            Public y As Integer
        End Structure
    End Class
End Namespace

Namespace TwainLib

    Public Class TwProtocol
        Public Const Major As Short = 1
        Public Const Minor As Short = 9
    End Class

#Region " Enums "
    <Flags()> Friend Enum TwDG As Short
        Control = &H1
        Image = &H2
        Audio = &H4
    End Enum

    Friend Enum TwDAT As Short
        Null = &H0
        Capability = &H1
        [Event] = &H2
        no = &H3
        Parent = &H4
        PendingXfers = &H5
        SetupMemXfer = &H6
        SetupFileXfer = &H7
        Status = &H8
        UserInterface = &H9
        XferGroup = &HA
        no = &HB
        CustomDSData = &HC
        DeviceEvent = &HD
        FileSystem = &HE
        PassThru = &HF

        ImageInfo = &H101
        ImageLayout = &H102
        ImageMemXfer = &H103
        ImageNativeXfer = &H104
        ImageFileXfer = &H105
        CieColor = &H106
        GrayResponse = &H107
        RGBResponse = &H108
        JpegCompression = &H109
        Palette8 = &H10A
        ExtImageInfo = &H10B

        SetupFileXfer2 = &H301
    End Enum

    Friend Enum TwMSG As Short
        Null = &H0
        [Get] = &H1
        GetCurrent = &H2
        GetDefault = &H3
        GetFirst = &H4
        GetNext = &H5
        [Set] = &H6
        Reset = &H7
        QuerySupport = &H8

        XFerReady = &H101
        CloseDSReq = &H102
        CloseDSOK = &H103
        DeviceEvent = &H104

        CheckStatus = &H201

        OpenDSM = &H301
        CloseDSM = &H302

        OpenDS = &H401
        CloseDS = &H402
        UserSelect = &H403

        DisableDS = &H501
        EnableDS = &H502
        EnableDSUIOnly = &H503

        ProcessEvent = &H601

        EndXfer = &H701
        StopFeeder = &H702

        ChangeDirectory = &H801
        CreateDirectory = &H802
        Delete = &H803
        FormatMedia = &H804
        GetClose = &H805
        GetFirstFile = &H806
        GetInfo = &H807
        GetNextFile = &H808
        Rename = &H809
        Copy = &H80A
        AutoCaptureDir = &H80B

        PassThru = &H901
    End Enum

    Friend Enum TwRC As Short
        Success = &H0
        Failure = &H1
        CheckStatus = &H2
        Cancel = &H3
        DSEvent = &H4
        NotDSEvent = &H5
        XferDone = &H6
        EndOfList = &H7
        InfoNotSupported = &H8
        DataNotAvailable = &H9
    End Enum

    Friend Enum TwCC As Short
        Success = &H0
        Bummer = &H1
        LowMemory = &H2
        NoDS = &H3
        MaxConnections = &H4
        OperationError = &H5
        BadCap = &H6
        BadProtocol = &H9
        BadValue = &HA
        SeqError = &HB
        BadDest = &HC
        CapUnsupported = &HD
        CapBadOperation = &HE
        CapSeqError = &HF
        Denied = &H10
        FileExists = &H11
        FileNotFound = &H12
        NotEmpty = &H13
        PaperJam = &H14
        PaperDoubleFeed = &H15
        FileWriteError = &H16
        CheckDeviceOnline = &H17
    End Enum

    Friend Enum TwOn As Short
        Array = &H3
        [Enum] = &H4
        One = &H5
        Range = &H6
        DontCare = -1
    End Enum

    Friend Enum TwType As Short
        Int8 = &H0
        Int16 = &H1
        Int32 = &H2
        UInt8 = &H3
        UInt16 = &H4
        UInt32 = &H5
        Bool = &H6
        Fix32 = &H7
        Frame = &H8
        Str32 = &H9
        Str64 = &HA
        Str128 = &HB
        Str255 = &HC
        Str1024 = &HD
        Str512 = &HE
    End Enum

    Friend Enum TwCap As Short
        XferCount = &H1
        ICompression = &H100
        IPixelType = &H101
        IUnits = &H102
        IXferMech = &H103
        BitDepth = &H112B
        ImageFileFormat = &H110C
        XResolution = &H1118
        YResolution = &H1119
        ImageWidth = &H1116
        ImageLength = &H1117
        SupportedSizes = &H1122
        XScaling = &H1124
        YScaling = &H1125

    End Enum

    Public Enum TwSS As Short
        TwSS_None = &H0
        TwSS_A4 = &H1
        TwSS_B5Letter = &H2
        TwSS_USLetter = &H3
    End Enum


    Public Enum TwColourType As Short
        twBW = 0
        twGREY = 1
        twRGB = 2
    End Enum

    Public Enum TwUnit As Short
        tw_PICAS = 2
        twCENTIMETERS = 1
        twINCHES = 0
        twPIXELS = 5
        twPOINTS = 3
        twTWIPS = 4
    End Enum

#End Region

    <StructLayout(LayoutKind.Sequential, Pack:=2, CharSet:=CharSet.Ansi)> Friend Class no
        Public Id As IntPtr
        Public Version As TwVersion
        Public ProtocolMajor As Short
        Public ProtocolMinor As Short
        Public SupportedGroups As Integer
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=34)> Public Manufacturer As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=34)> Public ProductFamily As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=34)> Public ProductName As String
    End Class

    <StructLayout(LayoutKind.Sequential, Pack:=2, CharSet:=CharSet.Ansi)> Friend Structure TwVersion
        Public MajorNum As Short
        Public MinorNum As Short
        Public Language As Short
        Public Country As Short
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=34)> Public Info As String
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwUserInterface
        Public ShowUI As Short
        Public ModalUI As Short
        Public ParentHand As IntPtr
    End Class

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwStatus
        Public ConditionCode As Short
        Public Reserved As Short
    End Class

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Structure TwEvent
        Public EventPtr As IntPtr
        Public Message As Short
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwImageInfo
        Public XResolution As Int32
        Public YResolution As Int32
        Public ImageWidth As Int32
        Public ImageLength As Int32
        Public SamplesPerPixel As Int16
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> Public BitsPerSample() As Short
        Public BitsPerPixel As Int16
        Public Planar As Boolean
        Public PixelType As Int16
        Public Compression As Short
    End Class

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwPendingXfers
        Public Count As Short
        Public EOJ As Integer
    End Class

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Structure TwFix32
        Public Whole As System.Int16
        Public Frac As System.UInt32

        Public Function ToFloat() As Single
            Dim frac_sng As Single
            frac_sng = System.Convert.ToSingle(Frac)
            Return CType(Whole + (CType(frac_sng, Single) / 65536.0F), Single)
        End Function

        Public Sub FromFloat(ByVal f As Single)
            Dim i As Int32 = CType(((f * 65536.0F) + 0.5F), Int32)
            Whole = System.Convert.ToInt16(i / 2 ^ 16)
            Frac = System.Convert.ToUInt32((i Or &HFFFF))
        End Sub

    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Structure TwFrame
        Public Left As TwFix32
        Public Top As TwFix32
        Public Right As TwFix32
        Public Bottom As TwFix32
    End Structure

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwImageLayout
        Public Frame As TwFrame
        Public DocumentNumber As Integer
        Public PageNumber As Integer
        Public FrameNumber As Integer
    End Class

    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class TwCapability
        Public Cap As Short
        Public ConType As Short
        Public Handle As IntPtr
        Public Sub TwCapability(ByVal capIn As TwCap)
            Cap = CType(capIn, Short)
            ConType = -1
        End Sub

        Public Sub New(ByVal capIn As TwCap, ByVal sval As Short, ByVal TWType As TwType)
            Cap = CType(capIn, Short)
            ConType = CType(TwOn.One, Short)
            Handle = Twain.GlobalAlloc(&H42, 6)
            Dim pv As IntPtr = Twain.GlobalLock(Handle)
            Marshal.WriteInt16(pv, 0, CType(TWType, Short))
            Marshal.WriteInt32(pv, 2, CType(sval, Short))
            Twain.GlobalUnlock(Handle)
        End Sub

        Public Sub Dispose()
            If Not Equals(Handle, IntPtr.Zero) Then
                Twain.GlobalFree(Handle)
            End If
        End Sub

        Protected Overrides Sub Finalize()
            If Not Equals(Handle, IntPtr.Zero) Then
                Twain.GlobalFree(Handle)
            End If
        End Sub
    End Class


End Namespace

Namespace ScanSoft
    <StructLayout(LayoutKind.Sequential, Pack:=2)> Friend Class BITMAPINFOHEADER
        Public biSize As Integer
        Public biWidth As Integer
        Public biHeight As Integer
        Public biPlanes As Short
        Public biBitCount As Short
        Public biCompression As Integer
        Public biSizeImage As Integer
        Public biXPelsPerMeter As Integer
        Public biYPelsPerMeter As Integer
        Public biClrUsed As Integer
        Public biClrImportant As Integer
    End Class


    Public Class scanToImage

        <DllImport("gdi32.dll", ExactSpelling:=True)> Friend Shared Function SetDIBitsToDevice(ByVal hdc As IntPtr, _
ByVal xdst As Integer, _
ByVal ydst As Integer, _
ByVal width As Integer, _
ByVal height As Integer, _
ByVal xsrc As Integer, _
ByVal ysrc As Integer, _
ByVal start As Integer, _
ByVal lines As Integer, _
ByVal bitsptr As IntPtr, _
ByVal bmiptr As IntPtr, _
ByVal color As Integer) As Integer
        End Function
        <DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalLock(ByVal handle As IntPtr) As IntPtr
        End Function
        <DllImport("kernel32.dll", ExactSpelling:=True)> Friend Shared Function GlobalFree(ByVal handle As IntPtr) As IntPtr
        End Function
        <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> Public Shared Sub OutputDebugString(ByVal outstr As String)
        End Sub

        Dim bmi As BITMAPINFOHEADER
        Dim bmprect As Rectangle
        Dim dibhand As IntPtr
        Dim bmpptr As IntPtr
        Dim pixptr As IntPtr

        Public Sub New(ByVal dibhandp As IntPtr)
            bmprect = New Rectangle(0, 0, 0, 0)
            dibhand = dibhandp
            bmpptr = GlobalLock(dibhand)
            pixptr = GetPixelInfo(bmpptr)

        End Sub

        Protected Function GetPixelInfo(ByVal bmpptr As IntPtr) As IntPtr
            bmi = New BITMAPINFOHEADER
            Marshal.PtrToStructure(bmpptr, bmi)
            bmprect.X = bmprect.Y = 0
            bmprect.Width = bmi.biWidth
            bmprect.Height = bmi.biHeight


            If (bmi.biSizeImage = 0) Then
                bmi.biSizeImage = Int((((bmi.biWidth * bmi.biBitCount) + 31) & Hex(Not (31))) / 2 ^ 3) * bmi.biHeight
            End If

            Dim p As Integer = bmi.biClrUsed
            If ((p = 0) And (bmi.biBitCount <= 8)) Then
                p = Int(1 * 2 ^ bmi.biBitCount)
            End If
            p = (p * 4) + bmi.biSize + CType(bmpptr.ToInt32, Integer)
            Return New IntPtr(p)
        End Function

        Public Function ImgToBitmap(ByVal dibhandp As IntPtr) As Bitmap
            bmprect = New Rectangle(0, 0, 0, 0)
            dibhand = dibhandp
            bmpptr = GlobalLock(dibhand)
            pixptr = GetPixelInfo(bmpptr)
            Dim TempBMP As Bitmap = New Bitmap(bmprect.Width, bmprect.Height)
            Dim TempGrap As Graphics = Graphics.FromImage(TempBMP)
            Dim hdc As IntPtr = TempGrap.GetHdc
            SetDIBitsToDevice(hdc, 0, 0, bmprect.Width, bmprect.Height, 0, 0, 0, bmprect.Height, pixptr, bmpptr, 0)
            TempGrap.ReleaseHdc(hdc)
            TempGrap.Dispose()
            GlobalFree(dibhand)
            dibhand = IntPtr.Zero
            Return (TempBMP)
        End Function
    End Class
End Namespace
 
Top Bottom