Scanner un document et l'enregistrer en jpeg avec twain

Description

Ce petit bout de code va permettre de scanner un document et de l'enregistrer en JPEG en ne demandant à l'utilisateur aucun paramètres. Ces paramètres seront prédéfinis par le développeur.

Source / Exemple :


Declare Function TWAIN_AcquireToClipboard Lib "EZTW32.DLL" (ByVal hwndApp&, ByVal wPixTypes&) As Integer
    Declare Function TWAIN_SetHideUI Lib "Eztw32.dll" Alias "TWAIN_SetHideUI" (ByVal ui As Long) As Long
    Declare Function TWAIN_OpenDefaultSource Lib "Eztw32.DLL" Alias "TWAIN_OpenDefaultSource" (ByVal hwnd As Long) As Integer
    Declare Function TWAIN_SetCurrentResolution Lib "Eztw32.dll" Alias "TWAIN_SetCurrentResolution" (ByVal neufdix As Double) As Long
    Declare Function TWAIN_SetCurrentPixelType Lib "Eztw32.dll" Alias "TWAIN_SetCurrentPixelType" (ByVal deux As Long) As Long
    Declare Function TWAIN_LoadSourceManager Lib "Eztw32.dll" Alias "TWAIN_LoadSourceManager" () As Long
    Declare Function TWAIN_SetCurrentUnits Lib "Eztw32.dll" Alias "TWAIN_SetCurrentUnits" (ByVal zero As Long) As Long
    Declare Function TWAIN_SetBitDepth Lib "Eztw32.dll" Alias "TWAIN_SetBitDepth" (ByVal zero As Long) As Long
    Declare Function TWAIN_OpenSourceManager Lib "EZTW32.DLL" Alias "TWAIN_OpenSourceManager" (ByVal hwnd As Long) As Long
    Declare Function TWAIN_CloseSource Lib "EZTW32.DLL" Alias "TWAIN_CloseSource" () As Long
    Declare Function TWAIN_SelectImageSource Lib "EZTW32.DLL" Alias "TWAIN_SelectImageSource" (ByVal hwnd As Long) As Integer
    Declare Function TWAIN_State Lib "EZTW32.DLL" Alias "TWAIN_State" () As Integer

FONCTION

        Dim iRetour As Long
        Dim s_cheminsauv As String
        Dim i As Integer

        Try

            TWAIN_CloseSource()
            TWAIN_LoadSourceManager()
            TWAIN_OpenSourceManager(frm.Handle.ToInt32)
            iRetour = TWAIN_OpenDefaultSource(frm.Handle.ToInt32)
            If iRetour = 0 Then
                iRetour = TWAIN_SelectImageSource(frm.Handle.ToInt32)
                If iRetour = 1 Then
                    iRetour = TWAIN_OpenDefaultSource(frm.Handle.ToInt32)
                End If
            End If

            If iRetour = 0 Then
                Exit Function
            End If

            If TWAIN_State() < 4 Then
                MsgBox("Impossible de paramêtrer le scanner")
                Exit Function
            Else
                'parametres du scanner
                TWAIN_SetCurrentUnits(0) 'DPI
                TWAIN_SetCurrentResolution(150) 'en points par pouces
                TWAIN_SetCurrentPixelType(2) ' Scan format 0 = B&W, 1 Grey, 2 RGB
                TWAIN_SetBitDepth(8) ' Bit Depth 1, 2, 4, 8 but depends on Pixeltype

                TWAIN_SetHideUI(True)
            End If

            'scan du document
            iRetour = TWAIN_AcquireToClipboard(frm.Handle.ToInt32, 32)

            If iRetour = 0 Then
                MsgBox("Le scan du document a échoué.", MsgBoxStyle.Critical, "Scanner")
                Exit Function
            End If

            TWAIN_CloseSource()
        Catch ex As Exception
            Exit Function
        End Try

        Try        

            pbImage.Image = System.Windows.Forms.Clipboard.GetDataObject.GetData(System.Windows.Forms.DataFormats.Bitmap) 'on la met dans une picture box

            s_cheminsauv = "c:\toto.jpg"

            i = 1
            If System.IO.File.Exists(s_cheminsauv) = True Then
                s_cheminsauv = Left(s_cheminsauv, s_cheminsauv.Length - 4)
                While System.IO.File.Exists(s_cheminsauv & i & ".jpg") = True
                    i = i + 1
                End While
                s_cheminsauv &= " "
                s_cheminsauv &= i
                s_cheminsauv &= ".jpg"
            End If

            pbImage.Image.Save(s_cheminsauv, System.Drawing.Imaging.ImageFormat.Jpeg)

        Catch ex As Exception
            MsgBox("Le document a bien été scanné," & Chr(13) & "mais une erreur s'est produite lors de la sauvegarde dans le dossier du candidat.", MsgBoxStyle.Critical, "Scanner")
        End Try

FIN DE LA FONCTION

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.