Loading...

Asynchronous TWAIN Scanning [UPDATED]

Example requests & Code samples for GdPicture.NET Toolkits.

Asynchronous TWAIN Scanning [UPDATED]

Postby Loïc » Tue Dec 14, 2010 3:48 pm

Hi here an example to scan from a separate thread by using a background worker:

Just start a new vb winform project, add a button then paste this whole code.

I got the snippet working with following configurations:

- Canon DR9080C (Success) feeder / Win7 64-bit / twaindsm.dll 2.1.2
- Canon DR9050C (Success) feeder / Win7 64-bit / twaindsm.dll 2.1.2
- HP Scanjet 5590 (Success) flatbed and feeder / Win7 64-bit / twaindsm.dll 2.1.2
- Epson perfection V200 Photo (Success) flatbed / Winxp 32-bit / twaindsm.dll 2.1.2
- Canonscan Lide 30 (Success) flatbed / Winxp 32-bit / twaindsm.dll 2.1.2
- Brother MFC J615W (Success) flatbed and feeder / Win7 64-bit / twaindsm.dll 2.1.2
- HP Office Pro 8500 (Success) flatbed and feeder / Win7 64-bit / twaindsm.dll 2.1.2

If you want to report working or non working configuration you are welcome to update this thread :)

Code: Select all
Option Strict On
Option Explicit On


Imports GdPicture

Public Class Form1

    Private m_GdPictureImaging As New GdPictureImaging
    Private WithEvents m_BackGroundWorker As New System.ComponentModel.BackgroundWorker
    Private m_hwnd As IntPtr = Me.Handle
    Private m_scanFolder As String = My.Application.Info.DirectoryPath
    Private m_useFeeder As Boolean = True 'Set to true to acquire from document feeder or false to acquire from flatbed


    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        m_GdPictureImaging.SetLicenseNumber("XXX") 'Please, replace XXXX by a valid demo or commercial license key.
        'Go to http://evaluation-gdpicture.com to get a 1 month trial key unlocking all features of the toolkit.
        m_GdPictureImaging.TwainLogStart("c:\gdpicture_twain.log")
        m_BackGroundWorker.WorkerReportsProgress = True
        m_BackGroundWorker.WorkerSupportsCancellation = False ' I suggest this
    End Sub


    Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
        m_GdPictureImaging.TwainUnloadSourceManager(m_hwnd)
    End Sub




    Private Sub m_BackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles m_BackGroundWorker.DoWork
        If m_GdPictureImaging.TwainOpenDefaultSource(m_hwnd) Then
            Dim scanCount As Integer = 0

            m_GdPictureImaging.TwainSetHideUI(True)
            m_GdPictureImaging.TwainSetIndicators(True)
            m_GdPictureImaging.TwainSelectFeeder(m_useFeeder)
            If (m_useFeeder) Then
                m_GdPictureImaging.TwainSetAutoFeed(True)
                m_GdPictureImaging.TwainSetAutoScan(True)
            End If


            Do
                If Not m_BackGroundWorker.CancellationPending Then
                    Dim ImageID As Integer = m_GdPictureImaging.TwainAcquireToGdPictureImage(IntPtr.Zero)
                    If ImageID <> 0 Then
                        scanCount += 1
                        m_BackGroundWorker.ReportProgress(scanCount, ImageID)
                    End If
                    If Not m_useFeeder Then
                        Exit Do 'we exit here in case of fladbed source acquisition
                    End If
                Else
                    Exit Do
                End If
            Loop While m_GdPictureImaging.TwainGetState > TwainStatus.TWAIN_SOURCE_ENABLED

            If scanCount > 0 Then
                MessageBox.Show(scanCount.ToString() + " pages have been acquired in: " + m_scanFolder)
            Else
                MessageBox.Show("Operation cancelled")
            End If

        Else
            MessageBox.Show("Unable to open the default TWAIN source. " + Chr(13) +
                            "Result code: " + m_GdPictureImaging.TwainGetLastResultCode.ToString() + Chr(13) +
                            "Condition code: " + m_GdPictureImaging.TwainGetLastConditionCode.ToString())
        End If
    End Sub


    Private Sub m_BackgroundWorker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles m_BackGroundWorker.ProgressChanged
        Dim ImageID As Integer = CInt(e.UserState)
        Call m_GdPictureImaging.SaveAsJPEG(ImageID, m_scanFolder + "\image" & Trim(Str(e.ProgressPercentage)) + ".jpg", 75)
        Call m_GdPictureImaging.ReleaseGdPictureImage(ImageID)
    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not m_BackGroundWorker.IsBusy Then
            m_GdPictureImaging.TwainSelectSource(m_hwnd)
            m_BackGroundWorker.RunWorkerAsync() 'do the scan
        Else
            MsgBox("Backgroung worker is currently busy!")
        End If
    End Sub
End Class


The app:
async_scan.zip
Asynchronous TWAIN Scanning with GdPicture.NET
(12.01 KiB) Downloaded 26 times
Loïc Carrère, support team.
www.orpalis.com
User avatar
Loïc
Site Admin
 
Posts: 4437
Joined: Tue Oct 17, 2006 10:48 pm
Location: France

Return to Example requests & Code samples For GdPicture.NET

Who is online

Users browsing this forum: No registered users and 0 guests