In the BiTWAIN.dll several window messages helps you to use the scanner:
WM_BI_SCANNING_DOCUMENT_FINISHED : After scanning only one page this message is received. The lParam value points to a structure that contains information about the scanning. It contains the scanned DIB and the file name if you scanned to a disk file.
WM_BI_TWAIN_CLOSE: This message is received when the TWAIN source is closed.
WM_BI_SCANNING_ERROR: This message is received when an error occurred. The lParam points to a structure containing the error code and error string.
WM_BI_FEEDER_EMPTY: This message is received when the feeder becomes empty during scanning.
WM_BI_PAGE_FINISHED: If you scan more than one page (batch scanning) after every page is scanned the WM_BI_PAGE_FINISHED message is received. The lParam value points to a structure that contains information about the scanning. It contains the scanned DIB and the file name.
The BiTWAIN.ocx uses events for handling messages:
Done : After scanning only one page this event is raised. The event parameters are the handle of the DIB and the name of the file that was created.
BatchPageDone: If you scan more than one page (batch scanning), after every page is scanned the BatchpageDone event is raised. The event parameters are the handle of the DIB and the name of the file that was created.
FeederIsEmpty: Raised when the feeder becomes empty during scanning.
TwError: Raised when an error occurred. The event parameters are the error code and error string.
Close: This event is raised when the TWAIN source is closed.
Important: You must be free the DIB if you don’t want to use it anymore. Use the GlobalFree API function in C++ and the ReleaseData method from BiTwain.ocx when using the ActiveX control.
[C++]
/* Processing TWAIN messages */
#include “BiTwain.h”
LRESULT CScanSampleDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
lpstScanningDocumentFinished lpstFinishDoc;
lpstScanningError lpstError;
char szMsg[128];
UINT i, j;
if (message == m_iBiTwainMsg)
{
switch (wParam)
{
case WM_BI_SCANNING_DOCUMENT_FINISHED :
// Scanned to memory
lpstFinishDoc =
(lpstScanningDocumentFinished)lParam;
GlobalFree(lpstFinishDoc->hDib);
break;
case WM_BI_SCANNING_ERROR :
lpstError = (lpstScanningError)lParam;
break;
case WM_BI_FEEDER_EMPTY:
AfxMessageBox(“The feeder is empty");
break;
case WM_BI_PAGE_FINISHED :
lpstFinishDoc =
(lpstScanningDocumentFinished)lParam;
GlobalFree(lpstFinishDoc->hDib);
break;
}
}
return CDialog::WindowProc(message, wParam, lParam);
}
[VB]
‘ Processing TWAIN events
Private Sub BiTwain_BatchPageDone(ByVal hDibOutput As Long, ByVal FileOutput As String)
If hDibOutput > 0 Then
BiTwain.ReleaseData hDibOutput
End If
End Sub
Private Sub BiTwain_FeederIsEmpty()
MsgBox "Feeder is empty"
End Sub
Private Sub BiTwain_TwError(ByVal ErrorCode As Long, ByVal ErrorStr As String)
Dim s As String
Dim i As Long
For i = 1 To Len(ErrorStr)
If Mid(ErrorStr, i, 1) <= " " Then
s = s + " "
Else
s = s + Mid(ErrorStr, i, 1)
End If
Next i
LBLIST.AddItem s
LBLIST.ListIndex = LBLIST.NewIndex
End Sub
[C#]
/* Processing TWAIN events */
private void BiTwain_BatchPageDone(object sender, AxBITWAINLib._DBiTwainEvents_BatchPageDoneEvent e)
{
if (e.hDibOutput > 0)
BiTwain.ReleaseData(e.hDibOutput);
}
private void BiTwain_FeederIsEmpty(object sender, System.EventArgs e)
{
MessageBox.Show("Feeder is empty", "TWAIN Sample",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
private void BiTwain_TwError(object sender, AxBITWAINLib._DBiTwainEvents_TwErrorEvent e)
{
string s = e.errorStr.Replace('\n', ' ');
MessageBox.Show (s, "TWAIN Sample",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}