WIA (Flatbed and ADF)
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,
- public Form2()
- {
- InitializeComponent();
-
- _deviceId = FindDefaultDeviceId();
-
- _deviceInfo = FindDevice(_deviceId);
-
- _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;
- }
-
- private string FindDefaultDeviceId()
- {
- string deviceId = Properties.Settings.Default.ScannerDeviceID;
- if (String.IsNullOrEmpty(deviceId))
- {
-
- 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:
- public List < Image > ScanPages(int dpi = 150, double width = 8.5, double height = 11)
- {
- Item item = _device.Items[1];
-
- SetDeviceItemProperty(ref item, 6146, 2);
- SetDeviceItemProperty(ref item, 6147, dpi);
- SetDeviceItemProperty(ref item, 6148, dpi);
- SetDeviceItemProperty(ref item, 6151, (int)(dpi * width));
- SetDeviceItemProperty(ref item, 6152, (int)(dpi * height));
- SetDeviceItemProperty(ref item, 4104, 8);
-
- List < Image > images = GetPagesFromScanner(ScanSource.DocumentFeeder, item);
- if (images.Count == 0)
- {
-
- 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();
-
- foreach(Image aa in obj)
- {
-
- aa.Save(@ "D:\ScanerUploadedFileWIA\" + DateTime.Now.ToString("
- yyyy - MM - dd HHmmss ") + ".jpeg ", ImageFormat.Jpeg);
- }
- }
- }
- enum ScanSource
- {
- DocumentFeeder = 1,
- Flatbed = 2,
- }
Note: Add the final. You have to set the properties of device ID, check the below Image,
This is the final solution with WIA. If you have any doubts and/or problems you can contact me any time.
All the best!