WIA (Flatbed and ADF)
Some important points you have to follow before starting the development:
Some important points you have to follow before starting the development:
- Before starting you should know something about WIA.
- Read about Windows image Acquisition properties and features.
- You have to import the Interop.WIA.dll
Steps you need to follow for development:
- Open your visual studio, go to file and create windows form application.
- Select language as c#.
- Give your application name as you want.
- Now go to the solution explorer right click and add form.
- Take one button control.

Now generate the click event of scan button.

Now you have to write the code for calling the scanner properties initializing the device and process , so you have to write this code into your form Initialization event.
Most Important point is You have to add reference of Interop.WIA.dll,
Most Important point is You have to add reference of Interop.WIA.dll,
- public Form2()
- {
- InitializeComponent();
- //Get default device Id
- _deviceId = FindDefaultDeviceId();
- //Find Device
- _deviceInfo = FindDevice(_deviceId);
- //Connect the device
- _device = _deviceInfo.Connect();
- }
- private DeviceInfo FindDevice(string deviceId)
- {
- DeviceManager manager = new DeviceManager();
- foreach(DeviceInfo info in manager.DeviceInfos)
- if (info.DeviceID == deviceId)
- return info;
- return null;
- }
- //finding the device Id here
- private string FindDefaultDeviceId()
- {
- string deviceId = Properties.Settings.Default.ScannerDeviceID;
- if (String.IsNullOrEmpty(deviceId))
- {
- // Select a scanner
- WIA.CommonDialog wiaDiag = new WIA.CommonDialog();
- Device d = wiaDiag.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
- if (d != null)
- {
- deviceId = d.DeviceID;
- Properties.Settings.Default.ScannerDeviceID = deviceId;
- Properties.Settings.Default.Save();
- }
- }
- return deviceId;
- }
Copy and paste below code:
Note: Add the final. You have to set the properties of device ID, check the below Image,

- public List < Image > ScanPages(int dpi = 150, double width = 8.5, double height = 11)
- {
- Item item = _device.Items[1];
- // configure item of the device
- SetDeviceItemProperty(ref item, 6146, 2); // greyscale
- SetDeviceItemProperty(ref item, 6147, dpi); // 150 dpi
- SetDeviceItemProperty(ref item, 6148, dpi); // 150 dpi
- SetDeviceItemProperty(ref item, 6151, (int)(dpi * width)); // set scan width
- SetDeviceItemProperty(ref item, 6152, (int)(dpi * height)); // set scan height
- SetDeviceItemProperty(ref item, 4104, 8); // bit depth
- // Detect if the ADF is loaded, if not use the flatbed
- List < Image > images = GetPagesFromScanner(ScanSource.DocumentFeeder, item);
- if (images.Count == 0)
- {
- // check the flatbed if ADF is not loaded, try from flatbed
- DialogResult dialogResult;
- do
- {
- List < Image > singlePage = GetPagesFromScanner(ScanSource.Flatbed, item);
- images.AddRange(singlePage);
- dialogResult = MessageBox.Show("Do you want to scan another page?", "ScanToEvernote", MessageBoxButtons.YesNo);
- }
- while (dialogResult == DialogResult.Yes);
- }
- return images;
- }
- private List < Image > GetPagesFromScanner(ScanSource source, Item item)
- {
- SetDeviceProperty(ref _device, 3088, (int) source);
- List < Image > images = new List < Image > ();
- int handlingStatus = GetDeviceProperty(ref _device, WIA_DPS_DOCUMENT_HANDLING_STATUS);
- if ((source == ScanSource.DocumentFeeder && handlingStatus == FEED_READY) || (source == ScanSource.Flatbed && handlingStatus == FLATBED_READY)) {
- do {
- ImageFile wiaImage = null;
- try {
- wiaImage = item.Transfer(WIA_FORMAT_JPEG);
- } catch (COMException ex)
- {
- if ((uint) ex.ErrorCode == WIA_ERROR_PAPER_EMPTY)
- break;
- else
- throw;
- }
- if (wiaImage != null)
- {
- System.Diagnostics.Trace.WriteLine(String.Format("Image is {0} x {1} pixels", (float) wiaImage.Width / 150, (float) wiaImage.Height / 150));
- Image image = ConvertToImage(wiaImage);
- images.Add(image);
- }
- }
- while (source == ScanSource.DocumentFeeder);
- }
- return images;
- }
- private static Image ConvertToImage(ImageFile wiaImage)
- {
- byte[] imageBytes = (byte[]) wiaImage.FileData.get_BinaryData();
- MemoryStream ms = new MemoryStream(imageBytes);
- Image image = Image.FromStream(ms);
- return image;
- }#region Get / set device properties
- private void SetDeviceProperty(ref Device device, int propertyID, int propertyValue)
- {
- foreach(Property p in device.Properties)
- {
- if (p.PropertyID == propertyID)
- {
- object value = propertyValue;
- p.set_Value(ref value);
- break;
- }
- }
- }
- private int GetDeviceProperty(ref Device device, int propertyID)
- {
- int ret = -1;
- foreach(Property p in device.Properties)
- {
- if (p.PropertyID == propertyID)
- {
- ret = (int) p.get_Value();
- break;
- }
- }
- return ret;
- }
- private void SetDeviceItemProperty(ref Item item, int propertyID, int propertyValue)
- {
- foreach(Property p in item.Properties)
- {
- if (p.PropertyID == propertyID)
- {
- object value = propertyValue;
- p.set_Value(ref value);
- break;
- }
- }
- }
- private int GetDeviceItemProperty(ref Item item, int propertyID)
- {
- int ret = -1;
- foreach(Property p in item.Properties)
- {
- if (p.PropertyID == propertyID)
- {
- ret = (int) p.get_Value();
- break;
- }
- }
- return ret;
- }#endregion
- private void button1_Click(object sender, EventArgs e)
- {
- List < Image > obj = ScanPages();
- //This code i written to upload the file
- foreach(Image aa in obj)
- {
- //aa.Save("D:\\ScanerUploadedFileWIA\\myfile.png _" + DateTime.Now.ToLongTimeString(), ImageFormat.Png);
- aa.Save(@ "D:\ScanerUploadedFileWIA\" + DateTime.Now.ToString("
- yyyy - MM - dd HHmmss ") + ".jpeg ", ImageFormat.Jpeg);
- }
- }
- }
- enum ScanSource
- {
- DocumentFeeder = 1,
- Flatbed = 2,
- }

This is the final solution with WIA. If you have any doubts and/or problems you can contact me any time.
All the best!
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Dave GatchalianPosted Apr 26, 2024, 1:35 AM
When I press the scan button, it always show the message scan another page but never scan anything. pls help
Saladino MiladPosted Mar 25, 2022, 5:35 PM
Value does not fall within the expected range. =>propertyValue 150
tosif24 AliPosted Dec 14, 2017, 5:39 AM
Plz help how to solve of this problems
tosif24 AliPosted Dec 14, 2017, 5:39 AM
Foreach (Property p in item.Properties) { if (p.PropertyID == propertyID) { object value = propertyValue; p.set_Value(ref value); break; } }
tosif24 AliPosted Dec 14, 2017, 5:38 AM
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in WIAWithFlatbedAndADF.exe Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
tosif24 AliPosted Dec 14, 2017, 5:38 AM
Private void SetDeviceItemProperty(ref Item item, int propertyID, int propertyValue) { foreach (Property p in item.Properties) { if (p.PropertyID == propertyID) { object value = propertyValue; p.set_Value(ref value); break; } } }
tosif24 AliPosted Dec 14, 2017, 5:37 AM
Hi., When i execute application i get error "Value does not fall in range" in SetDeviceItemProperty function. can you help me how to resolve this issue?
angappan sPosted Oct 25, 2017, 1:21 AM
Hi., When i execute application i get error "Value does not fall in range" in SetDeviceItemProperty function. can you help me how to resolve this issue?
Manish PandeyPosted Apr 7, 2016, 8:51 AM
Thank you Rajshekhar Pampad
Rajshekhar PampadPosted Apr 6, 2016, 11:14 AM
Nice one Very Helpful!!!