WIA Scanner Class

My dear diary... I haven't written to you for quite a while, but I have my excuse: I was busy working. Actually I still am, but I'd like to share something with you that I made for a hobby project recently. I wanted to make my scanner work from .Net code, there is a standard way for it using the Windows Image Acquisition Library but frankly it could be easier to use and more integrated into the .Net framework... So I made a wrapping class that should make it way easier to operate a WIA-compatible scanner device. Device management, setup and scanning have been simplified so it's literally a few lines of code to make it work. The current version works with flatbed scanners only, document feeders are slightly harder to program and even harder to test without having access to one, so I had to skip that for now. The code will work with those devices too, but you can scan only single page documents. To use the class and your scanner, first you need to add the WIA object reference to your project...

Add the WIA object reference to your projectAdd the WIA object reference to your project

In Visual Studio 2010 you will get this error on build: Interop type 'WIA.CommonDialogClass' cannot be embedded. Use the applicable interface instead.
To avoid it, you must change the WIA object properties by setting Embed Interop Types to False.

It will make a local copy named Interop.WIA.dll on build but in return it will work. In Visual Studio 2008 you don't need this trick. When you're done with this and your project builds properly, add the Scanner.cs file which uses the "Utility" namespace, feel free to change it.

Using the class couldn't be easier in most cases. Just select one of your scanner devices, set up its basic parameters for the document you want to scan, then you can get the resulting image file with ease. You may choose to use the scanner's GUI for device selecting and image acquiring (where you can create preview, cropping, and my HP even does edge detection quite well) or you can go without the GUI. Let me demonstrate its simplicity with a little source code which is also included in the file.

private void ScannerTest()
{
    //We are going to scan, yay!
    Scanner scanner = new Scanner();
 
    //Connect to the first available scanner in the system.
    if(scanner.Connect() == true)
    {
        //Set some real basic properties for color image, 300 DPI.
        scanner.SetProperties(ScannerImageIntents.Color, 300, 300);
 
        //Scan the document without GUI, but show the progress bar...
        Bitmap bitmap = scanner.Scan(true);
 
        //It wasn't cancelled, was it?
        if(bitmap != null)
        {
            //Save the image file! You can choose from various image formats.
            scanner.SaveBitmap(bitmap, "C:\\doc.png", ScannerImageTypes.png);
        }
    }
}

The package also contains a text file with the standard WIA Properties (from MSDN) because you will need to set them by using their constants. The WIA definitions header file has even more stuff in it, if you feel like going pro. :-) Have fun with it, let me know if it helped you out!

WIA Scanner ClassSource code in C# for .Net Framework 2.0

« Apple Keynotes in Full Screen
Meanwhile in the Man Cave... »