public bool Open()
 {
   if(IsOpen == false)
   {
     fWia = new WIA.DeviceManager();
   }
   return IsOpen;
 }
 private void searchDevices()
 {
     devices = new List <string>();
     manager = new WIA.DeviceManager();
     foreach (WIA.DeviceInfo devInfo in manager.DeviceInfos)
     {
         devices.Add(devInfo.DeviceID);
     }
 }
Example #3
0
        public static WiaDevice GetDeviceFromID(string deviceID)
        {
            var manager = new WIA.DeviceManager();

            foreach (WIA.DeviceInfo dev in manager.DeviceInfos)
                if (dev.DeviceID == deviceID)
                    return new WiaDevice(dev.Connect());

            return null;
        }
 /// <summary>
 /// Gets the list of available WIA devices.
 /// </summary>
 /// <returns></returns>
 public static List<string> GetDevices()
 {
     List<string> devices = new List<string>();
     WIA.DeviceManager manager = new WIA.DeviceManager();
     foreach (WIA.DeviceInfo info in manager.DeviceInfos)
     {
         devices.Add(info.DeviceID);
     }
     return devices;
 }
        /// <summary>
        /// Gets the list of available WIA devices.
        /// </summary>
        /// <returns></returns>
        public static List <string> GetDevices()
        {
            List <string> devices = new List <string>();

            WIA.DeviceManager manager = new WIA.DeviceManager();
            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                devices.Add(info.DeviceID);
            }
            return(devices);
        }
Example #6
0
        //public static Dictionary<string, string> GetDevices()
        //{
        //  Dictionary<string, string> devices = new Dictionary<string, string>();
        //  WIA.DeviceManager manager = new WIA.DeviceManager();

        //  foreach (WIA.DeviceInfo info in manager.DeviceInfos)
        //  {
        //    System.Windows.Forms.MessageBox.Show(info.DeviceID);
        //    foreach (WIA.Property p in info.Properties)
        //    {
        //      if (p.Name == "Name")
        //      {
        //        devices.Add(info.DeviceID, p.get_Value());
        //      }
        //    }
        //  }
        //  return devices;
        //}


        private void PopulateDeviceList()
        {
            WIA.DeviceManager manager = new WIA.DeviceManager();

            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                if (info.Type == WIA.WiaDeviceType.ScannerDeviceType)
                {
                    foreach (WIA.Property p in info.Properties)
                    {
                        if (p.Name == "Name")
                        {
                            listDevices.Items.Add(p.get_Value());
                        }
                    }
                }
            }



            if (listDevices.Items.Count == 0)
            {
                lblAvailDevices.Content = "Available devices: None found";
            }
            else
            {
                lblAvailDevices.Content = "Available devices:";
                foreach (string item in listDevices.Items)
                {
                    if (item == DefaultDevice)
                    {
                        listDevices.SelectedItem = item;
                        break;
                    }
                }
                if (listDevices.SelectedIndex == -1)
                {
                    listDevices.SelectedIndex = 0;
                }
                btnDefaultDevice.IsEnabled = true;
            }
            btnRefreshDevices.IsEnabled = true;
        }
Example #7
0
        public static Dictionary <string, string> GetDevices()
        {
            Dictionary <string, string> devices = new Dictionary <string, string>();

            WIA.DeviceManager manager = new WIA.DeviceManager();

            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                foreach (WIA.Property p in info.Properties)
                {
                    if (p.Name == "Name")
                    {
                        devices.Add(info.DeviceID, p.get_Value());
                        //Console.WriteLine(p.Name + ":" + p.get_Value());
                    }
                }
            }
            return(devices);
        }
        /// <summary>
        /// Scan a single image
        /// </summary>
        public byte[] ScanSingle(ScannerInfo source)
        {
            WIA.Device wiaDevice = source.GetDevice();
            // Manager
            WIA.DeviceManager wiaManager = new WIA.DeviceManager();

            try
            {
                // Get items
                WIA.Item wiaItem = wiaDevice.Items[1];
                int      inColor = 2, dpi = 300;
                wiaItem.Properties["6146"].set_Value((int)inColor);//Item MUST be stored in a variable THEN the properties must be set.
                wiaItem.Properties["6147"].set_Value(dpi);
                wiaItem.Properties["6148"].set_Value(dpi);

                //var imageFile = (ImageFile)(new CommonDialog()).ShowTransfer(wiaItem, "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}", false);

                var imageFile = wiaItem.Transfer("{b96b3caf-0728-11d3-9d7b-0000f81ef32e}") as WIA.ImageFile;
                //private static ImageFormat memoryBMP = new ImageFormat(new Guid("{b96b3caa-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat bmp = new ImageFormat(new Guid("{b96b3cab-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat emf = new ImageFormat(new Guid("{b96b3cac-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat wmf = new ImageFormat(new Guid("{b96b3cad-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat jpeg = new ImageFormat(new Guid("{b96b3cae-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat png = new ImageFormat(new Guid("{b96b3caf-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat gif = new ImageFormat(new Guid("{b96b3cb0-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat tiff = new ImageFormat(new Guid("{b96b3cb1-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat exif = new ImageFormat(new Guid("{b96b3cb2-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat photoCD = new ImageFormat(new Guid("{b96b3cb3-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat flashPIX = new ImageFormat(new Guid("{b96b3cb4-0728-11d3-9d7b-0000f81ef32e}"));
                // private static ImageFormat icon = new ImageFormat(new Guid("{b96b3cb5-0728-11d3-9d7b-0000f81ef32e}"));
                return(imageFile.FileData.get_BinaryData());

                //Bitmap image1 = (Bitmap)Image.FromFile(@"D:\omr\tagilid.bmp", true);
                //MemoryStream ms = new MemoryStream();
                //image1.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                //return ms.ToArray();
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// Get a list of devices
        /// </summary>
        public List <ScannerInfo> GetWiaDevices()
        {
            WIA.DeviceManager  mgr    = new WIA.DeviceManager();
            List <ScannerInfo> retVal = new List <ScannerInfo>();

            foreach (WIA.DeviceInfo info in mgr.DeviceInfos)
            {
                if (info.Type == WIA.WiaDeviceType.ScannerDeviceType)
                {
                    foreach (WIA.Property p in info.Properties)
                    {
                        if (p.Name == "Name")
                        {
                            retVal.Add(new ScannerInfo(((WIA.IProperty)p).get_Value().ToString(), info.DeviceID));
                        }
                    }
                }
            }
            return(retVal);
        }
    /// <summary>
    /// Use scanner to scan an image (scanner is selected by its unique id).
    /// </summary>
    /// <param name="scannerName"></param>
    /// <returns>Scanned images.</returns>
    public static List <Image> Scan(string scannerId)
    {
        List <Image> images = new List <Image>();

        bool hasMorePages = true;

        while (hasMorePages)
        {
            // select the correct scanner using the provided scannerId parameter
            WIA.DeviceManager manager = new WIA.DeviceManager();
            WIA.Device        device  = null;
            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                if (info.DeviceID == scannerId)
                {
                    // connect to scanner
                    device = info.Connect();
                    break;
                }
            }

            // device was not found
            if (device == null)
            {
                // enumerate available devices
                string availableDevices = "";
                foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                {
                    availableDevices += info.DeviceID + "\n";
                }

                // show error with available devices
                throw new Exception("The device with provided ID could not be found. Available Devices:\n" + availableDevices);
            }

            WIA.Item item = device.Items[1] as WIA.Item;

            try
            {
                // scan image
                WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                WIA.ImageFile     image           = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);

                // save to temp file
                string fileName = Path.GetTempFileName();
                File.Delete(fileName);
                image.SaveFile(fileName);
                image = null;

                // add file to output list
                images.Add(Image.FromFile(fileName));
            }
            catch (Exception exc)
            {
                throw exc;
            }
            finally
            {
                item = null;

                //determine if there are any more pages waiting
                WIA.Property documentHandlingSelect = null;
                WIA.Property documentHandlingStatus = null;

                foreach (WIA.Property prop in device.Properties)
                {
                    if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                    {
                        documentHandlingSelect = prop;
                    }

                    if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                    {
                        documentHandlingStatus = prop;
                    }
                }

                // assume there are no more pages
                hasMorePages = false;

                // may not exist on flatbed scanner but required for feeder
                if (documentHandlingSelect != null)
                {
                    // check for document feeder
                    if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                    {
                        hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                    }
                }
            }
        }

        return(images);
    }
Example #11
0
        /// <summary>
        /// Gets the list of available WIA devices.
        /// </summary>
        /// <returns></returns>
        public static List <Device> GetDevices()
        {
            List <Device> devices = new List <Device>();

            WIA.DeviceManager manager = new WIA.DeviceManager();
            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                Device device = new Device();

                switch (info.Type)
                {
                case WIA.WiaDeviceType.CameraDeviceType:
                    device.Type = "Camera";
                    break;

                case WIA.WiaDeviceType.ScannerDeviceType:
                    device.Type = "Scanner";
                    break;

                case WIA.WiaDeviceType.VideoDeviceType:
                    device.Type = "Video";
                    break;

                default:
                    device.Type = "Unknown";
                    break;
                }

                device.FullDeviceId = info.DeviceID;
                device.DeviceId     = info.DeviceID.Split('\\')[0];
                List <DeviceProperty> deviceProperties = new List <DeviceProperty>();
                foreach (WIA.Property property in info.Properties)
                {
                    DeviceProperty deviceProperty = new DeviceProperty();
                    deviceProperty.IsReadonly   = property.IsReadOnly;
                    deviceProperty.IsVector     = property.IsVector;
                    deviceProperty.Name         = property.Name;
                    deviceProperty.PropertyId   = property.PropertyID.ToString();
                    deviceProperty.PropertyType = property.Type;
                    deviceProperty.Value        = property.get_Value();

                    try
                    {
                        switch (property.SubType)
                        {
                        case WIA.WiaSubType.FlagSubType:
                            deviceProperty.SubType = "Flag";
                            break;

                        case WIA.WiaSubType.ListSubType:
                            deviceProperty.SubType = "List";
                            break;

                        case WIA.WiaSubType.RangeSubType:
                            deviceProperty.SubType = "Range";
                            break;

                        case WIA.WiaSubType.UnspecifiedSubType:
                            deviceProperty.SubType = "Unspecified";
                            break;

                        default:
                            deviceProperty.SubType = string.Empty;
                            break;
                        }

                        deviceProperty.SubTypeDefault = property.SubTypeDefault;
                        deviceProperty.SubTypeMax     = property.SubTypeMax;
                        deviceProperty.SubTypeMin     = property.SubTypeMin;
                        deviceProperty.SubTypeStep    = property.SubTypeStep;

                        foreach (var v in property.SubTypeValues)
                        {
                            deviceProperty.SubTypeValues.Add(v);
                        }
                    }
                    catch (Exception ex)
                    {
                        deviceProperty.SubType = string.Empty;
                    }

                    deviceProperties.Add(deviceProperty);
                }
                device.Properties = deviceProperties;

                devices.Add(device);
            }
            return(devices);
        }
Example #12
0
        /// <summary>
        /// Use scanner to scan an image (scanner is selected by its unique id).
        /// </summary>
        /// <param name="scannerName"></param>
        /// <returns>Scanned images.</returns>
        public static List<Image> Scan(string scannerId)
        {
            List<Image> images = new List<Image>();
            bool hasMorePages = true;
            while (hasMorePages)
            {
                // select the correct scanner using the provided scannerId parameter
                WIA.DeviceManager manager = new WIA.DeviceManager();
                WIA.Device device = null;
                foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.DeviceID == scannerId)
                    {
                        // connect to scanner
                        device = info.Connect();
                        break;
                    }
                }
                // device was not found
                if (device == null)
                {
                    // enumerate available devices
                    string availableDevices = "";
                    foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                    {
                        availableDevices += info.DeviceID + "\n";
                    }

                    // show error with available devices
                    throw new Exception("The device with provided ID could not be found. Available Devices:\n" + availableDevices);
                }
                WIA.Item item = device.Items[1] as WIA.Item;
                try
                {
                    // scan image
                    WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                    WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);

                    // save to temp file
                    string fileName = Path.GetTempFileName();
                    File.Delete(fileName);
                    image.SaveFile(fileName);
                    image = null;
                    // add file to output list
                    images.Add(Image.FromFile(fileName));
                }
                catch (Exception exc)
                {
                    throw exc;
                }
                finally
                {
                    item = null;
                    //determine if there are any more pages waiting
                    WIA.Property documentHandlingSelect = null;
                    WIA.Property documentHandlingStatus = null;
                    foreach (WIA.Property prop in device.Properties)
                    {
                        if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                            documentHandlingSelect = prop;
                        if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                            documentHandlingStatus = prop;
                    }
                    // assume there are no more pages
                    hasMorePages = false;
                    // may not exist on flatbed scanner but required for feeder
                    if (documentHandlingSelect != null)
                    {
                        // check for document feeder
                        if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                        {
                            hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                        }
                    }
                }
            }
            return images;
        }
        public void Scan(WiaDocumentHandlingType handlingType      = WiaDocumentHandlingType.Feeder,
                         WiaDocumentHandlingType?duplexOrOtherMode = null,
                         List <string> imagesFromScanner           = null)
        {
            if (imagesFromScanner == null)
            {
                imagesFromScanner = new List <string>();
            }
            SelectDevice();

            if (SelectedDevice == null)
            {
                return;
            }


            bool hasMorePages = true;


            // select the correct scanner using the provided scannerId parameter
            WIA.DeviceManager manager = new WIA.DeviceManager();
            WIA.Device        device  = null;
            try
            {
                if (manager.DeviceInfos == null || manager.DeviceInfos.Count < 1)
                {
                    throw new WiaScannerDeviceNotFoundException((string)Application.Current.FindResource("NemaDostupnihSkeneraUzvicnik"));
                }

                foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.DeviceID == SelectedDevice.DeviceID)
                    {
                        // connect to scanner
                        device = info.Connect();
                        break;
                    }
                }

                if (handlingType == WiaDocumentHandlingType.Feeder)
                {
                    if (duplexOrOtherMode != null)
                    {
                        SetProperty(device.Properties, (uint)WiaProperty.DocumentHandlingSelect, (uint)(handlingType | duplexOrOtherMode));
                    }
                    else
                    {
                        SetProperty(device.Properties, (uint)WiaProperty.DocumentHandlingSelect, (uint)handlingType);
                    }
                }
            }
            catch (Exception ex)
            {
                device = null;
            }

            while (hasMorePages)
            {
                // device was not found
                if (device == null)
                {
                    throw new WiaScannerDeviceNotFoundException((string)Application.Current.FindResource("OdabraniSkenerNijeDostupanUzvicnik"));
                }

                WIA.Item item = device.Items[1] as WIA.Item;
                try
                {
                    PreviousPropertyValues.Clear();

                    foreach (var property in PropertyValues)
                    {
                        WIA.Property prop = GetProperty(item.Properties, property.Key);
                        PreviousPropertyValues.Add(property.Key, prop?.get_Value());

                        var supported = prop.SubTypeValues;
                        SetProperty(item.Properties, property.Key, property.Value);
                    }


                    // scan image
                    WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();


                    WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, true);

                    WIA.ImageFile duplexImage = null;
                    if (duplexOrOtherMode == WiaDocumentHandlingType.Duplex)
                    {
                        Thread.Sleep(100);
                        duplexImage = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, true);
                    }
                    // save to temp file
                    string fileName = Path.GetTempFileName();
                    if (image != null)
                    {
                        File.Delete(fileName);
                        image.SaveFile(fileName);
                        Marshal.FinalReleaseComObject(image);
                        image = null;
                        // add file to output list
                        imagesFromScanner.Add(fileName);
                    }
                    if (duplexImage != null)
                    {
                        string duplexFileName = Path.GetTempFileName();
                        File.Delete(duplexFileName);
                        duplexImage.SaveFile(duplexFileName);
                        Marshal.FinalReleaseComObject(duplexImage);
                        duplexImage = null;
                        // add file to output list
                        imagesFromScanner.Add(duplexFileName);
                    }


                    item = null;

                    WIA.Property documentHandlingSelect = null;
                    WIA.Property documentHandlingStatus = null;
                    foreach (WIA.Property prop in device.Properties)
                    {
                        if (prop.PropertyID == (uint)WiaProperty.DocumentHandlingSelect)
                        {
                            documentHandlingSelect = prop;
                        }
                        if (prop.PropertyID == (uint)WiaProperty.DocumentHandlingStatus)
                        {
                            documentHandlingStatus = prop;
                        }
                    }
                    hasMorePages = false;

                    if (documentHandlingSelect != null)
                    {
                        var propId            = documentHandlingSelect.PropertyID;
                        var propValue         = documentHandlingSelect.get_Value();
                        var propValueBitCheck = (propValue & ((uint)WiaDocumentHandlingType.Feeder));
                        if (propValueBitCheck != 0)
                        {
                            var statusId            = documentHandlingStatus.PropertyID;
                            var statusValue         = documentHandlingStatus.get_Value();
                            var statusValueBitCheck = (statusValue & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY);
                            hasMorePages = (statusValueBitCheck != 0);
                        }
                    }


                    //Thread.Sleep(500);
                }
                catch (WiaScannerDeviceNotFoundException exc)
                {
                    throw exc;
                }
                catch (IOException exc)
                {
                    throw exc;
                }
                catch (COMException exc)
                {
                    if (imagesFromScanner?.Count() < 1)
                    {
                        if ((uint)exc.ErrorCode == 0x80210003)
                        {
                            throw new WiaScannerInsertPaperException(exc.Message);
                        }
                        else
                        {
                            throw exc;
                        }
                    }
                    else // U slucaju da su pokupljene slike, kad dodje do poruke o poslednjoj skeniranoj prekidamo izvrsavanje
                    {
                        return;
                    }
                }
                catch (Exception exc)
                {
                    throw exc;
                }
            }
        }
 public WIA.Device GetDevice()
 {
     WIA.DeviceManager manager = new WIA.DeviceManager();
     return(manager.DeviceInfos.OfType <WIA.DeviceInfo>().FirstOrDefault(o => o.DeviceID == this.Id).Connect());
 }
 public WiaDataSourceManager()
 {
   fWia = null;
 }
        /// <summary>
        /// Start scan
        /// </summary>
        public void ScanAsync(ScannerInfo source)
        {
            if (source == null)
            {
                return;
            }

            WIA.Device wiaDevice = source.GetDevice();
            // Manager
            WIA.DeviceManager wiaManager = new WIA.DeviceManager();

            bool hasMorePages = true;

            while (hasMorePages)
            {
                try
                {
                    // Get items
                    WIA.Item wiaItem = wiaDevice.Items[1];
                    int      inColor = 1;
                    int      dpi     = 300;
                    wiaItem.Properties["6146"].set_Value((int)inColor);//Item MUST be stored in a variable THEN the properties must be set.
                    wiaItem.Properties["6147"].set_Value(dpi);
                    wiaItem.Properties["6148"].set_Value(dpi);

                    //var imageFile = (ImageFile)(new CommonDialog()).ShowTransfer(wiaItem, "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", false); //wiaItem.Transfer("{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}") as WIA.ImageFile;

                    //var imageFile = wiaItem.Transfer("{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}") as WIA.ImageFile;
                    var imageFile = wiaItem.Transfer("{b96b3cab-0728-11d3-9d7b-0000f81ef32e}") as WIA.ImageFile;


                    if (this.ScanCompleted != null)
                    {
                        this.ScanCompleted(this, new ScanCompletedEventArgs(imageFile.FileData.get_BinaryData()));
                    }
                }
                catch (Exception)
                {
                    break;
                }
                finally
                {
                    //determine if there are any more pages waiting
                    WIA.Property documentHandlingSelect = null;
                    WIA.Property documentHandlingStatus = null;

                    foreach (WIA.Property prop in wiaDevice.Properties)
                    {
                        if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                        {
                            documentHandlingSelect = prop;
                        }
                        if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                        {
                            documentHandlingStatus = prop;
                        }
                    }
                    hasMorePages = false; //assume there are no more pages
                    if (documentHandlingSelect != null)
                    //may not exist on flatbed scanner but required for feeder
                    {
                        //check for document feeder
                        if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                        {
                            hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                        }
                    }

                    if (hasMorePages && this.SingleOnly)
                    {
                        hasMorePages = false;
                    }
                }
            }
        }
Example #17
0
        public static List <Image> Scan(string scannerName)
        {
            List <Image> images       = new List <Image>();
            bool         hasMorePages = true;
            bool         paperEmpty   = false;

            while (hasMorePages)
            {
                Dictionary <string, string> devices = WiaScanner.GetDevices();
                var scannerId             = devices.FirstOrDefault(x => x.Value == scannerName).Key;
                WIA.DeviceManager manager = new WIA.DeviceManager();
                WIA.Device        device  = null;
                // select the correct scanner using the provided scannerId parameter
                foreach (WIA.DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.DeviceID == scannerId && info.Type == WIA.WiaDeviceType.ScannerDeviceType)
                    {
                        // connect to scanner
                        device = info.Connect();
                        break;
                    }
                }

                // device was not found
                if (device == null)
                {
                    // enumerate available devices
                    string availableDevices = "";
                    foreach (var d in devices)
                    {
                        availableDevices += d.Value + "\n";
                    }

                    // show error with available devices
                    throw new Exception("The device with provided ID could not be found. Available Devices:\n" + availableDevices);
                }

                WIA.Item item = SetPaperSetting(device);

                try
                {
                    // scan image
                    WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                    WIA.ImageFile     image           = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatJPEG, true);

                    //WIA.ImageProcess imp = new WIA.ImageProcess();  // use to compress jpeg.
                    //imp.Filters.Add(imp.FilterInfos["Convert"].FilterID);
                    ////imp.Filters[1].Properties["FormatID"].set_Value(wiaFormatJPEG);
                    //imp.Filters[1].Properties["FormatID"].set_Value(wiaFormatJPEG);
                    //imp.Filters[1].Properties["Quality"].set_Value(80); // 1 = low quality, 100 = best
                    //image = imp.Apply(image);  // apply the filters

                    // get a tempfile path
                    string fileName = Path.GetTempFileName();
                    // delete any existing file first
                    File.Delete(fileName);
                    // save to temp file
                    image.SaveFile(fileName);
                    // make the original (wia) image null
                    image = null;
                    // get system.drawing image from temporary file and add file to output list
                    images.Add(Image.FromFile(fileName, true));

                    //var imageBytes = (byte[])image.FileData.get_BinaryData();
                    //var stream = new MemoryStream(imageBytes);
                    //images.Add(Image.FromStream(stream));
                }
                catch (System.Runtime.InteropServices.COMException cx)
                {
                    string ex           = string.Empty;
                    int    comErrorCode = GetWIAErrorCode(cx);
                    if (comErrorCode > 0)
                    {
                        ex = GetErrorCodeDescription(comErrorCode);
                    }
                    if (comErrorCode == 3 && images.Count == 0)
                    {
                        throw new Exception(ex);
                    }
                    else if (comErrorCode == 3 && images.Count > 0)
                    {
                        paperEmpty = true;
                    }
                }

                finally
                {
                    item = null;
                    // assume there are no more pages
                    hasMorePages = false;
                    if (!paperEmpty)
                    {
                        //determine if there are any more pages waiting
                        WIA.Property documentHandlingSelect = null;
                        WIA.Property documentHandlingStatus = null;

                        foreach (WIA.Property prop in device.Properties)
                        {
                            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                            {
                                documentHandlingSelect = prop;
                            }

                            if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                            {
                                documentHandlingStatus = prop;
                            }
                        }

                        // may not exist on flatbed scanner but required for feeder
                        if (documentHandlingSelect != null)
                        {
                            // check for document feeder
                            if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
                            {
                                hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
                            }
                        }
                    }
                }
            }
            return(images);
        }
 public void Close()
 {
   fWia = null;
 }