Scan Multiple Pages w/Flatbed & settings don't stick

Questions, comments and suggestions concerning VintaSoft Twain .NET SDK.

Moderator: Alex

Post Reply
mlewis
Posts: 2
Joined: Tue May 11, 2010 11:22 pm

Scan Multiple Pages w/Flatbed & settings don't stick

Post by mlewis »

I've written the below method to allow our scanning module to scan documents from a flatbed scanner. The function also allows for prompting the user for additional document pages if they chose to do so.

All of that works very well.

The problem I'm running into is after the first image is acquired via VSTwain1.AcquireModal(), all subsequent images ignore the previous settings. For example, if my settings are to scan in black and white at 200 dpi the first page will scan at those settings, but the 2nd, 3rd, etc scans will be in full color at 300 dpi (default settings).

Is there some magic that happens with AcquireModal() when scanning from a flatbed?

I've been looking through the SDK docs and the forums and I can't find anything that mentions the settings being reset.

What can I do to prevent this? Am I going about this the wrong way? This happens on my HP J4580 and the HP PSC 1401 we have which is flatbed only.

Here's the code:

Code: Select all

Private Shared Function ScanFromFlatbed(ByVal strFileName As String, Optional ByVal bShowSelectDevice As Boolean = True) As Boolean
	If bShowSelectDevice AndAlso Not VSTwain1.SelectSource() Then
		Return False
	End If

	MessageBox.Show("Place the " & IIf(ScanningMultiplePages, "first ", String.Empty) & "document on the scanning device then click OK to start scanning.", "Waiting for Document", MessageBoxButtons.OK, MessageBoxIcon.Information)

	VSTwain1.ShowUI = False
	VSTwain1.DisableAfterAcquire = True
	VSTwain1.TransferMode = TransferMode.Native
	VSTwain1.MaxImages = 1
	VSTwain1.AutoCleanBuffer = True
	VSTwain1.OpenDataSource()

	DetectDeviceType()

	SetCapabilities()

	VSTwain1.XferCount = 1

	VSTwain1.PdfDocumentInfo.Author = "MyApplicationNameHere"
	VSTwain1.PdfDocumentInfo.Creator = "VintaSoftTWAIN.NET v" & VSTwain1.Version
	VSTwain1.PdfImageCompression = PdfImageCompression.Auto
	VSTwain1.PdfMultiPage = True

	Dim bDoneScanning As Boolean = False
	Dim bRescanning As Boolean = False

	While Not bDoneScanning
		If VSTwain1.AcquireModal() Then
			Try
				VSTwain1.SaveImage(0, strFileName)
			Catch ex As TwainException
				WriteToDebugFile(ex.ToString)
				MessageBox.Show(ex.Message, "Scanning Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
			End Try

			If ScanningMultiplePages Then ' prompt the user to see if there are more pages left to scan
				If MessageBox.Show("Do you have any more pages to append to this document?", "Multi-page Flatbed Scanning", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
					' we need to pause so the user can place the next original on the scanner
					MessageBox.Show("Place the next document on the scanning device then click OK to continue.", "Waiting for Next Document", MessageBoxButtons.OK, MessageBoxIcon.Information)
					bRescanning = True
				Else
					bDoneScanning = True
					bRescanning = False

				End If

			Else
				bDoneScanning = True

			End If

		ElseIf bRescanning Then ' ignore the acquire failure
			bRescanning = False

		Else
			Return False

		End If

	End While

	Return True

End Function
NOTE: The above code assumes the library has already been registered and that StartDevice() has already been called.


One part of the code that you may notice is this:

Code: Select all

ElseIf bRescanning Then ' ignore the acquire failure
         bRescanning = False
Which I had to do because the next time AcquireModal() was called it would immediately return false and nothing would happen, but calling it again would result in another scan. This may be part of the problem, but I don't understand why that happens or what is going on when it does occur so any help would be appreciated.



Thanks,
Mark
Alex
Site Admin
Posts: 2305
Joined: Thu Jul 10, 2008 2:21 pm

Re: Scan Multiple Pages w/Flatbed & settings don't stick

Post by Alex »

Hello Mark,

Your code has logical mistake - AcquireModal method must be called in a loop, first call will acquire image from flatbed and returns true, the second call will close session and returns false.

Here is your code:

Code: Select all

...
If VSTwain1.AcquireModal() Then
    ...
End If
...
and here is correct code:

Code: Select all

...
While VSTwain1.AcquireModal() Then
    ...
End While
...
About settings: You should set parameters of scanning for each session, i.e. you should set parameters after each OpenDataSource method.

Please read the "Getting Started" section in the documentation.

Best regards, Alexander
mlewis
Posts: 2
Joined: Tue May 11, 2010 11:22 pm

Re: Scan Multiple Pages w/Flatbed & settings don't stick

Post by mlewis »

Ah, I see. That might be a good thing to add to the documentation of the method.

I thought calling it in a loop was optional and on top of that I had no idea that it had to be called a second time to close a session which is also undocumented in that method.

I've restructured the code to take this into account and everything seems to be working well.

Thank you for your help.


-Mark
Alex
Site Admin
Posts: 2305
Joined: Thu Jul 10, 2008 2:21 pm

Re: Scan Multiple Pages w/Flatbed & settings don't stick

Post by Alex »

Ok, we will update the documentation to make it more clearly, thank you for suggestion.

Best regards, Alexander
Post Reply