TwainOpenSource async

Discussions about TWAIN & WIA scanning in GdPicture.NET using GdPictureImaging.
Post Reply
Jefferson
Posts: 6
Joined: Tue May 29, 2012 7:45 pm

TwainOpenSource async

Post by Jefferson » Wed Feb 20, 2013 1:07 am

i cannot get TwainOpenSource to work when using async... open default works, but not opensource.
i altered the example code you've provided... basically i just need the user to select from a listbox what scanner they want to use, then open that source and scan, however it hangs on the TwainOpenSource.

Option Strict On
Option Explicit On

Imports GdPicture9

Public Class Form1

Private oGdPictureImaging As New GdPictureImaging
Private WithEvents m_BackGroundWorker As New System.ComponentModel.BackgroundWorker
Private m_scanFolder As String = My.Application.Info.DirectoryPath

Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim LicMgr As New LicenseManager
LicMgr.RegisterKEY("xxxxxx")
LicMgr.RegisterKEY("xxxxxx")
m_BackGroundWorker.WorkerReportsProgress = True
m_BackGroundWorker.WorkerSupportsCancellation = False ' I suggest this

DisplayTwainDevices()

End Sub
Private Sub DisplayTwainDevices()
For i As Integer = 1 To oGdPictureImaging.TwainGetSourceCount(Me.Handle)
Me.lstDevices.Items.Add(oGdPictureImaging.TwainGetSourceName(Me.Handle, i))
Next
End Sub

Private Sub m_BackgroundWorker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles m_BackGroundWorker.DoWork
Dim hwnd As IntPtr = IntPtr.Zero 'To enfore the TWAIN controler to create its own windows proxy in this SAME thread.

If oGdPictureImaging.TwainOpenSource(hwnd, e.Argument.ToString) Then
If oGdPictureImaging.TwainOpenDefaultSource(hwnd) Then
Dim scanCount As Integer = 0
Dim useFeeder As Boolean = chkUseFeeder.Checked
Dim duplex As Boolean = chkDuplex.Checked

oGdPictureImaging.TwainSetHideUI(chkHideGUI.Checked)
oGdPictureImaging.TwainSetIndicators(chkEnableIndicator.Checked)
oGdPictureImaging.TwainSelectFeeder(useFeeder)

If useFeeder Then
oGdPictureImaging.TwainSetAutoFeed(True)
oGdPictureImaging.TwainSetAutoScan(True)
End If

If duplex Then
oGdPictureImaging.TwainEnableDuplex(True)
End If

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

If scanCount > 0 Then
MessageBox.Show(scanCount.ToString() + " pages have been acquired in: " + m_scanFolder)
Else
MessageBox.Show("Operation failed cancelled. Please check status!")
End If
oGdPictureImaging.TwainCloseSource()
Else
MessageBox.Show("Unable to open the default TWAIN source. " + Chr(13) + _
"Result code: " + oGdPictureImaging.TwainGetLastResultCode.ToString() + Chr(13) + _
"Condition code: " + oGdPictureImaging.TwainGetLastConditionCode.ToString())
End If
Else
MessageBox.Show("Operation cancelled - no source selected")
End If
oGdPictureImaging.TwainUnloadSourceManager(hwnd)
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 oGdPictureImaging.SaveAsJPEG(ImageID, m_scanFolder + "\image" & Trim(Str(e.ProgressPercentage)) + ".jpg", 75)
Call oGdPictureImaging.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
If chkGenLog.Checked Then
oGdPictureImaging.TwainLogStart("c:\gdpicture_twain.log")
End If

oGdPictureImaging.TwainSetModalUI(chkModalUI.Checked)


If chkTwain19.Checked Then
oGdPictureImaging.TwainSetDSMPath("c:\windows\twain_32.dll")
Else
oGdPictureImaging.TwainSetDSMPath("") 'the higehr DSM will be automatically loaded
End If

m_BackGroundWorker.RunWorkerAsync(lstDevices.Items(lstDevices.SelectedIndex)) 'do the scan

Else
MessageBox.Show("Backgroung worker is currently busy!")
End If
End Sub
End Class

User avatar
Loïc
Site Admin
Posts: 5881
Joined: Tue Oct 17, 2006 10:48 pm
Location: France
Contact:

Re: TwainOpenSource async

Post by Loïc » Thu Feb 21, 2013 8:58 pm

Hello,

Unfortunately I can't do anything with your code.
So I've made a simple try by altering this demo provided on the asynchronous scanning topic and it worked like a charm.

So in the demo of viewtopic.php?t=4073

Replace:

Code: Select all

If m_GdPictureImaging.TwainOpenDefaultSource(hwnd) Then
by

Code: Select all

 
 Dim sourceName As String = m_GdPictureImaging.TwainGetDefaultSourceName(hwnd)
 If m_GdPictureImaging.TwainOpenSource(hwnd, sourceName) Then
Hope this helps!

Kind regards,

Loïc

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests