In this post I will show how to use Windows Image Acquisition (WIA) interface to scan images and convert them to PDF using converter from my previous post.
There is a nice intro article on WIA written by Scott Hanselman several years ago. Please read it, I referred to it several times over the years. There is also some information available on Wikipedia.
So what is WIA:
WIA is a Microsoft driver model and application programming interface (API) for Microsoft Windows Me and later Windows operating systems that enables graphics software to communicate with imaging hardware such as scanners, digital cameras and Digital Video-equipment. It was first introduced in 2000 as part of Windows Me, and continues to be the standard imaging device and API model through successive Windows versions. It is implemented as an on-demand service in Windows XP and later Windows operating systems.
If you are developing for Windows XP you will need to download this tool. It requires one time administrative installation on a client machine. I created a little batch file to install these files:
If you are developing for Windows Vista or Windows 7 you need to know this:
“In Windows XP, WIA runs in the LocalSystem context. Because of the security ramifications of running a service as LocalSystem whereby a buggy driver or malicious person would have unrestricted access to the system, the WIA service in Windows Server 2003 and Windows Vista operates in the LocalService context. This can result in compatibility issues when using a driver designed for Windows XP.”
“Windows Vista has the WIA Automation library built-in. Also, WIA supports push scanning and multi-image scanning. Push scanning allows initiating scans and adjusting scanning parameters directly from the scanner control panel. Multi-image scanning allows you to scan several images at once and save them directly as separate files. However, video content support is removed from WIA for Windows Vista. Microsoft recommends using the newer Windows Portable Devices API.”
Now on to the hardware. Since in my project I needed to scan multiple pages at once, I used Fujitsu fi-5110C and newer model (works just as well) fi-6110. The problem is that in Windows XP for some reason these scanners don’t report correctly the “Empty Tray” status. So I had to assume some things for multi page scanning. The logic is simple:
- scan until no exception.
- when exception occurs checks if anything is scanned.
- if pages were scanned then simply exit.
- if no pages were scanned then report a problem.
Here is a snippet responsible for this:
That’s multi page scanning in Windows XP, unlike earlier statement from Wiki that it is supported only in Vista.
Another not so straight forward part is how to initialize the scanner. By default when you call WIA API to scan it loads manufacturers interface. In my case I wanted to save all the settings using internally developed user form and from then on pass these settings on to a scanner every time scanner is used.
I created a class to store scanner settings.
And then apply these settings in the following manner:
Another annoying window is selecting the scanner itself:
This is taken care by saving device id into local user settings and then reading it off:
Here is the rest of the code. Scanner initialization was a little tricky without fully understanding the WIA API. So I had to resort to several hacks and empirical methods :).
Below are the other two classes you will need to scan images.
Now to be able to easily use these classes I created a helper class which combines scanning functionality with PDF conversion:
Here are couple of overloads. One lets you scan into a file, the other one returns stream of PDF data, which you may use in your program.
That’s it for scanning.