In This Topic
You should do the following for correct cancelation of image acqisition process:
- Call the Device_CancelImageTransfer method.
- Wait for the Device_ScanCanceled event if you acquire images asynchronously.
- Wait while the Device_AcquireImageModal method returns False if you acquire images in modal loop.
Some high speed scanners have internal buffer and scanners cache the acquired images in the buffer if scanner is faster than application which processes acquired images. Images, which are cached in internal buffer, will be lost if you cancel the image acquisition process. You can
pause image acquisition process (instead of cancelling image acquisition) if you need to keep all acquired images.
Example: Here is an example that shows how to cancel current
asynchronous image transfer.
Private VSTwain1 As New VintaSoftTwain()
Private _isScanFinished As Boolean
Private _isScanCanceled As Boolean
''' <summary>
''' This method scans one image and cancels image scan.
''' </summary>
Private Sub ScanOneImageAndCancelScan()
_isScanFinished = False
_isScanCanceled = False
VSTwain1.DeviceManager_IsTwain2Compatible = True
' open the device manager
If Not VSTwain1.DeviceManager_Open() Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' get the device index
Dim deviceName As String = "KODAK Scanner: i5000"
Dim deviceIndex As Integer = VSTwain1.DeviceManager_FindDevice(deviceName)
If deviceIndex = -1 Then
Throw New ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
End If
' select the device by index
VSTwain1.DeviceManager_SelectedDeviceIndex = deviceIndex
' disable device UI
VSTwain1.Device_ShowUI = False
' specify that device must be closed after scan
VSTwain1.Device_DisableAfterAcquire = True
' open the device
If Not VSTwain1.Device_Open Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' specify that 2 images must be acquired from scanner
VSTwain1.Device_XferCount = 2
' subscribe to the device events
AddHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
AddHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
AddHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
AddHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
' run asynchronous image acqusition
VSTwain1.Device_AcquireImage()
' wait while feeder will be stopped
While Not _isScanFinished
Application.DoEvents()
End While
' unsubscribe from device events
RemoveHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
RemoveHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
RemoveHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
RemoveHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
' close the device
VSTwain1.Device_Close()
If Not _isScanCanceled Then
Throw New ApplicationException("Scan is NOT canceled.")
End If
End Sub
Private Sub VSTwain1_ImageAcquired()
Console.WriteLine("Image is acquired.")
' cancel image scan
VSTwain1.Device_CancelImageTransfer()
End Sub
Private Sub VSTwain1_ScanCompleted()
Console.WriteLine("Scan is completed.")
_isScanFinished = True
End Sub
Private Sub VSTwain1_ScanFailed(errorString As String)
Console.WriteLine(String.Format("Scan is failed: {0}.", errorString))
_isScanFinished = True
End Sub
Private Sub VSTwain1_ScanCanceled()
_isScanCanceled = True
Console.WriteLine("Scan is canceled.")
_isScanFinished = True
End Sub
Example: Here is an example that shows how to cancel current
synchronous image transfer.
Private VSTwain1 As New VintaSoftTwain()
Private _isScanFinished As Boolean
Private _isScanCanceled As Boolean
''' <summary>
''' This method scans one image and cancels image scan.
''' </summary>
Private Sub ScanOneImageAndCancelScan()
_isScanFinished = False
_isScanCanceled = False
VSTwain1.DeviceManager_IsTwain2Compatible = True
' open the device manager
If Not VSTwain1.DeviceManager_Open() Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' get the device index
Dim deviceName As String = "KODAK Scanner: i5000"
Dim deviceIndex As Integer = VSTwain1.DeviceManager_FindDevice(deviceName)
If deviceIndex = -1 Then
Throw New ApplicationException(String.Format("Device '{0}' is not found.", deviceName))
End If
' select the device by index
VSTwain1.DeviceManager_SelectedDeviceIndex = deviceIndex
' disable device UI
VSTwain1.Device_ShowUI = False
' specify that device must be closed after scan
VSTwain1.Device_DisableAfterAcquire = True
' open the device
If Not VSTwain1.Device_Open Then
Console.WriteLine(VSTwain1.errorString)
Exit Sub
End If
' specify that 2 images must be acquired from scanner
VSTwain1.Device_XferCount = 2
' subscribe to the device events
AddHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
AddHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
AddHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
AddHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
' run asynchronous image acqusition
VSTwain1.Device_AcquireImage()
' wait while feeder will be stopped
While Not _isScanFinished
Application.DoEvents()
End While
' unsubscribe from device events
RemoveHandler VSTwain1.DeviceImageAcquired, AddressOf VSTwain1_ImageAcquired
RemoveHandler VSTwain1.DeviceScanCompleted, AddressOf VSTwain1_ScanCompleted
RemoveHandler VSTwain1.DeviceScanFailed, AddressOf VSTwain1_ScanFailed
RemoveHandler VSTwain1.DeviceScanCanceled, AddressOf VSTwain1_ScanCanceled
' close the device
VSTwain1.Device_Close()
If Not _isScanCanceled Then
Throw New ApplicationException("Scan is NOT canceled.")
End If
End Sub
Private Sub VSTwain1_ImageAcquired()
Console.WriteLine("Image is acquired.")
' cancel image scan
VSTwain1.Device_CancelImageTransfer()
End Sub
Private Sub VSTwain1_ScanCompleted()
Console.WriteLine("Scan is completed.")
_isScanFinished = True
End Sub
Private Sub VSTwain1_ScanFailed(errorString As String)
Console.WriteLine(String.Format("Scan is failed: {0}.", errorString))
_isScanFinished = True
End Sub
Private Sub VSTwain1_ScanCanceled()
_isScanCanceled = True
Console.WriteLine("Scan is canceled.")
_isScanFinished = True
End Sub
See Also