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.
WM_BI_SCAN_ABORTED: This message is received, when the user aborts the scanning. For example, if the user closes the user interface of the driver. This message is also received when the user clicks on the CANCEL button of the Black Ice specific user interface dialog.
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.
ScanningAborted: This event is raised when the user aborts the scanning. For example the user closes the user interface of the driver. This event is also raised when the user clicks on the CANCEL button of the Black Ice specific user interface dialog.
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);
}
[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);
}