This application automatically scans the document or photo from the scanner in some particular interval given by the user. We can easily and quickly scan bulk amount of documents (i.e 10,000 pages) with this application.
Normally we can scan the documents using scanner application or WIA (Windows Image Acquisition) driver. For scanning the documents it takes too much time to scan upto 1000 pages.
A basic Windows application runs on a single thread usually referred to as UI thread. This UI thread is responsible for creating/painting all the controls and upon which the code execution takes place.
The BackgroundWorker is designed to let you run heavy or long operations on a separate thread to that of the UI. If you were to run a lengthy process on the UI thread, your UI will most likely freeze until the process completes.
The background worker thread is there to help to offload long running function calls to the background so that the interface will not freeze.
Suppose you have something that takes 5 sec to compute when you click a button. During that time, the interface will appear 'frozen': you won't be able to interact with it.
The difference in behavior is because the call to the background worker thread will not execute in the same thread as the interface, freeing it to continue its normal work. If you used the background worker thread instead, the button event would setup the worker thread and return immediately. That will allow the interface to keep accepting new events like other button clicks.
Implementing Automatic Scanning
DoWork event is the starting point for a BackgroundWorker. This event is fired when the RunWorkerAsync method is called. In this event handler, we call our code that is being processed in the background where our application is still doing some other work.
Thread Process Code:
private void bgwScan_DoWork(object sender, DoWorkEventArgs e)
if (newDoc == 0)
newDoc = 1;
for (int k = 1; k <= 10 * (int)nudTime.Value; k++)
bgwScan.ReportProgress((int)(k / (int)nudTime.Value));
if (k == 10 * (int)nudTime.Value)
newDoc = 0;
The above code asynchronously calls the
ScanDoc() method until pressing the stop button.
Image Scanning Code:
ScanDoc() is the method for scanning the documents from the Scanner.
private void ScanDoc()
CommonDialogClass commonDialogClass = new CommonDialogClass();
Device scannerDevice = commonDialogClass.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, false);
if (scannerDevice != null)
Item scannnerItem = scannerDevice.Items;
AdjustScannerSettings(scannnerItem, (int)nudRes.Value, 0, 0, (int)nudWidth.Value, (int)nudHeight.Value, 0, 0, cmbCMIndex);
object scanResult = commonDialogClass.ShowTransfer(scannnerItem, WIA.FormatID.wiaFormatTIFF, false);
if (scanResult != null)
ImageFile image = (ImageFile)scanResult;
string fileName = "";
var files = Directory.GetFiles(txtPath.Text, "*.tiff");
string f = ((files.Max(p1 => Int32.Parse(Path.GetFileNameWithoutExtension(p1)))) + 1).ToString();
fileName = txtPath.Text + "\\" + f + ".tiff";
catch (Exception ex)
fileName = txtPath.Text + "\\" + "1.tiff";
picScan.ImageLocation = fileName;
Scanner Settings Code:
AdjustScannerSettings is the method to change scanner color mode, document size and crop size.
private static void AdjustScannerSettings(IItem scannnerItem, int scanResolutionDPI, int scanStartLeftPixel, int scanStartTopPixel,
int scanWidthPixels, int scanHeightPixels, int brightnessPercents, int contrastPercents, int colorMode)
const string WIA_SCAN_COLOR_MODE = "6146";
const string WIA_HORIZONTAL_SCAN_RESOLUTION_DPI = "6147";
const string WIA_VERTICAL_SCAN_RESOLUTION_DPI = "6148";
const string WIA_HORIZONTAL_SCAN_START_PIXEL = "6149";
const string WIA_VERTICAL_SCAN_START_PIXEL = "6150";
const string WIA_HORIZONTAL_SCAN_SIZE_PIXELS = "6151";
const string WIA_VERTICAL_SCAN_SIZE_PIXELS = "6152";
const string WIA_SCAN_BRIGHTNESS_PERCENTS = "6154";
const string WIA_SCAN_CONTRAST_PERCENTS = "6155";
SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_RESOLUTION_DPI, scanResolutionDPI);
SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_START_PIXEL, scanStartLeftPixel);
SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_START_PIXEL, scanStartTopPixel);
SetWIAProperty(scannnerItem.Properties, WIA_HORIZONTAL_SCAN_SIZE_PIXELS, scanWidthPixels);
SetWIAProperty(scannnerItem.Properties, WIA_VERTICAL_SCAN_SIZE_PIXELS, scanHeightPixels);
SetWIAProperty(scannnerItem.Properties, WIA_SCAN_BRIGHTNESS_PERCENTS, brightnessPercents);
SetWIAProperty(scannnerItem.Properties, WIA_SCAN_CONTRAST_PERCENTS, contrastPercents);
SetWIAProperty(scannnerItem.Properties, WIA_SCAN_COLOR_MODE, colorMode);
private static void SetWIAProperty(IProperties properties, object propName, object propValue)
Property prop = properties.get_Item(ref propName);
Saving Image Code:
SaveImageToTiff is the method to save the scanned image to tiff image format file.
private static void SaveImageToTiff(ImageFile image, string fileName)
ImageProcess imgProcess = new ImageProcess();
object convertFilter = "Convert";
string convertFilterID = imgProcess.FilterInfos.get_Item(ref convertFilter).FilterID;
SetWIAProperty(imgProcess.Filters[imgProcess.Filters.Count].Properties, "FormatID", WIA.FormatID.wiaFormatTIFF);
image = imgProcess.Apply(image);
Further Uses of the Code
In this code can used to consequently load the records from Network Database when the records made changed, it can be done with the