WIA (Flatbed and ADF)
Some important point you have to fallow before to start the development :-
- Before to start you should know something about WIA.
- Read about Windows image Acquisition properties and feature.
- You have to import the Interop.WIA.dll
Steps you need to fallow 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 initialized 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();
//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;
- }
- 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 doubt and problems you can contact with me any time.
all the best.
No comments:
Post a Comment