Exemple #1
0
        /// <summary>
        /// Seleciona a impressora a ser utilziada para digitalizar um documento
        /// O usuário deve escolher onde salvar a imagem
        /// </summary>
        /// <param name="dialog"></param>
        public static void ScanningToDisk()
        {
            ////http://www.andrealveslima.com.br/blog/index.php/2015/12/16/como-escanear-documentos-com-o-c-digitalizacao-de-documentos/
            CommonDialogClass dialog = new WIA.CommonDialogClass();

            WIA.Device scanner = dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, true, false);
            if (scanner != null)
            {
                dialog.ShowAcquisitionWizard(scanner);
            }
        }
Exemple #2
0
        public ScanAuto()
        {
            DefaultScaner = XmlHelper.GetConfig("DefaultScan");
            LocalSavePath = XmlHelper.GetConfig("LocalPath");
            DeviceManagerClass manager = new DeviceManagerClass();
            Device WiaDev = null;
            CommonDialogClass devCdc = new WIA.CommonDialogClass();
            if (string.IsNullOrEmpty(DefaultScaner))
            {
                WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                if (WiaDev != null)
                {
                    XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID);
                }
            }
            else
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    if (info.DeviceID == DefaultScaner)
                    {

                        WiaDev = info.Connect();
                        break;
                    }
                }
            }
            manager.RegisterEvent("{0C5E2143-FD9B-490B-9AD5-7637A403566B}", WiaDev.DeviceID);

            manager.OnEvent += (eventID, deviceID, itemID) =>
            {
                Item item = WiaDev.Items[1];

                CommonDialogClass cdc = new WIA.CommonDialogClass();
                ImageFile imageFile = cdc.ShowTransfer(item,
                "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}",
                true) as ImageFile;

                if (imageFile != null)
                {
                    var buffer = imageFile.FileData.get_BinaryData() as byte[];
                    //imageFile.SaveFile(Path.Combine(LocalSavePath, DateTime.Now.Millisecond + ".jpg"));
                    using (MemoryStream ms = new MemoryStream())
                    {
                        ms.Write(buffer, 0, buffer.Length);
                        imgs.Add(System.Drawing.Image.FromStream(ms));
                    }
                }
            };
        }
        public WIA.ImageFile ScanAndSaveOnePage()
        {
            WIA.CommonDialog  Dialog1        = new WIA.CommonDialogClass();
            WIA.DeviceManager DeviceManager1 = new WIA.DeviceManagerClass();
            System.Object     Object1        = null;
            System.Object     Object2        = null;
            WIA.Device        Scanner        = null;

            try
            {
                Scanner = Dialog1.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);
            }
            catch
            {
                MessageBox.Show("请确认是否联系设备");
                return(null);

                throw;
            }
            WIA.Item Item1 = Scanner.Items[1];
            setItem(Item1, "4104", 24);
            setItem(Item1, "6146", 2);
            setItem(Item1, "6147", 150);
            setItem(Item1, "6148", 150);
            setItem(Item1, "6151", 150 * 8.5);
            setItem(Item1, "6152", 150 * 11);

            WIA.ImageFile    Image1        = new WIA.ImageFile();
            WIA.ImageProcess ImageProcess1 = new WIA.ImageProcess();
            Object1 = (Object)"Convert";
            ImageProcess1.Filters.Add(ImageProcess1.FilterInfos.get_Item(ref Object1).FilterID, 0);

            Object1 = (Object)"FormatID";
            Object2 = (Object)WIA.FormatID.wiaFormatBMP;
            ImageProcess1.Filters[1].Properties.get_Item(ref Object1).set_Value(ref Object2);

            Object1 = null;
            Object2 = null;

            Image1 = (WIA.ImageFile)Item1.Transfer(WIA.FormatID.wiaFormatBMP);

            return(Image1);

            //string DestImagePath = @"C:\test.bmp";
            //File.Delete(DestImagePath);
            //Image1.SaveFile(DestImagePath);
        }
Exemple #4
0
        public List<Image> StartScan()
        {
            imgs = null;
            imgs = new List<Image>();

            //var tempPath=Path.GetTempPath();

            ImageFile imageFile;
            DeviceManagerClass manager = new DeviceManagerClass();
            Device WiaDev = null;
            CommonDialogClass devCdc = new WIA.CommonDialogClass();
            if (string.IsNullOrEmpty(DefaultScaner))
            {
                WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                if (WiaDev != null)
                {
                    XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID);
                }
            }
            else
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    if (info.DeviceID == DefaultScaner)
                    {

                        WiaDev = info.Connect();
                        break;
                    }
                }
            }

            if (WiaDev == null)
            {
                try
                {
                    WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                    if (WiaDev != null)
                    {
                        XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID); ;
                    }
                    else
                    {
                        throw new Exception("请确认扫描仪是否正常连接!");
                        //MessageBox.Show("请确认扫描仪是否正常连接!");
                        //return null;
                    }
                }
                catch
                {
                    return null;
                }

            }

            Property documentHandlingSelect1 = null;

            foreach (Property prop in WiaDev.Properties)
            {
                if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                {
                    documentHandlingSelect1 = prop;
                    object obj = new object();
                    //obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);

                    obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);
                    documentHandlingSelect1.set_Value(ref obj);

                }
                //else if (prop.PropertyID == 3013)
                //{
                //    object val = 1;
                //    prop.set_Value(ref val);
                //}
                //Pages
                else if (prop.PropertyID == 3096)
                {
                    object val = 1;
                    prop.set_Value(ref val);
                }
                else if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                {
                    documentHandlingSelect1 = prop;

                }
            }
            int dpi = 200;

            Item item = WiaDev.Items[1];
            foreach (WIA.Property itemProperty in item.Properties)
            {
                //IProperty tempProperty;
                Object tempNewProperty;

                if (itemProperty.Name.Equals("Horizontal Resolution"))
                {
                    tempNewProperty = dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                else if (itemProperty.Name.Equals("Vertical Resolution"))
                {
                    tempNewProperty = dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }//Colour intent
                else if (itemProperty.PropertyID == 6146)
                {
                    object val2 = 4;
                    ((IProperty)itemProperty).set_Value(ref val2);
                }
                //else if (itemProperty.PropertyID == 4110)
                //{
                //    object val2 = 1100;
                //    ((IProperty)itemProperty).set_Value(ref val2);
                //}
                //else if (itemProperty.PropertyID == 4120)
                //{
                //    object val1 = 2;
                //    ((IProperty)itemProperty).set_Value(ref val1);
                //}
                //else if (itemProperty.PropertyID == 4102)
                //{
                //    object val1 = 1;

                //    ((IProperty)itemProperty).set_Value(ref val1);
                //}
                //4104 _ Bits Per Pixel
                //else if (itemProperty.PropertyID == 4112)
                //{
                //    object val1 = 2481;

                //    ((IProperty)itemProperty).set_Value(ref val1);
                //}
                else if (itemProperty.Name.Equals("Horizontal Extent"))
                {
                    tempNewProperty = 8.27 * dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                else if (itemProperty.Name.Equals("Vertical Extent"))
                {
                    tempNewProperty = 11.69 * dpi;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
            }

            CommonDialogClass cdc = new WIA.CommonDialogClass();
            try
            {
                imageFile = cdc.ShowTransfer(item,wiaFormatJPEG, false) as ImageFile;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                imageFile = null;

            }

            while (imageFile != null)
            {

                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                //imageFile.SaveFile(Path.Combine(LocalSavePath, DateTime.Now.Millisecond + ".bmp"));
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    imgs.Add(System.Drawing.Image.FromStream(ms));
                    ms.Close();
                    ms.Dispose();
                }
                buffer = null;
                imageFile = null;
                //MethodInvoker mm = delegate
                //{
                //    try
                //    {
                //        var tmp = Path.GetTempPath();
                //        foreach (string d in Directory.GetFileSystemEntries(tmp))
                //        {
                //            if (d.Contains("img") && File.Exists(d))
                //            {
                //                FileInfo fi = new FileInfo(d);
                //                if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                //                    fi.Attributes = FileAttributes.Normal;
                //                File.Delete(d);//直接删除其中的文件
                //            }
                //        }
                //    }
                //    catch (Exception ex)
                //    {

                //    }
                //};

                //mm.BeginInvoke(null, null);
                try
                {
                    imageFile = cdc.ShowTransfer(item, wiaFormatJPEG, false) as ImageFile;
                }
                catch (Exception ex)
                {

                    continue;
                    //return imgs;
                    //throw new Exception(ex.Message);
                }
            }
            return imgs.ToList();
        }
Exemple #5
0
        public List<Image> StartScan()
        {
            ImageFile imageFile;
            DeviceManagerClass manager = new DeviceManagerClass();
            Device WiaDev = null;
            CommonDialogClass devCdc = new WIA.CommonDialogClass();

            if (string.IsNullOrEmpty(DefaultScaner))
            {
                WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                if (WiaDev != null)
                {
                    XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID);
                }
            }
            else
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    if (info.DeviceID == DefaultScaner)
                    {

                        WiaDev = info.Connect();
                        break;
                    }
                }
            }

            if (WiaDev == null)
            {
                try
                {
                    WiaDev = devCdc.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
                    if (WiaDev != null)
                    {
                        XmlHelper.UpdateConfig("DefaultScan", WiaDev.DeviceID); ;
                    }
                    else
                    {
                        throw new Exception("请确认扫描仪是否正常连接!");
                        //MessageBox.Show("请确认扫描仪是否正常连接!");
                        //return null;
                    }
                }
                catch
                {
                    return null;
                }

            }

            Property documentHandlingSelect1 = null;

            foreach (Property prop in WiaDev.Properties)
            {
                if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
                {
                    documentHandlingSelect1 = prop;
                    object obj = new object();
                    //obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);

                    obj = (WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER);
                    documentHandlingSelect1.set_Value(ref obj);

                }
                //else if (prop.PropertyID == 3013)
                //{
                //    object val = 1;
                //    prop.set_Value(ref val);
                //}
                //Pages
                else if (prop.PropertyID == 3096)
                {
                    object val = 1;
                    prop.set_Value(ref val);
                }
                else if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
                {
                    documentHandlingSelect1 = prop;

                }
            }

            Item item = WiaDev.Items[1];
            foreach (WIA.Property itemProperty in item.Properties)
            {
                //IProperty tempProperty;
                Object tempNewProperty;

                if (itemProperty.Name.Equals("Horizontal Resolution"))
                {
                    tempNewProperty = 100;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                else if (itemProperty.Name.Equals("Vertical Resolution"))
                {
                    tempNewProperty = 100;
                    ((IProperty)itemProperty).set_Value(ref tempNewProperty);
                }
                //else if (itemProperty.Name.Equals("Horizontal Extent"))
                //{
                //    //tempNewProperty = 619;
                //    //((IProperty)itemProperty).set_Value(ref tempNewProperty);
                //}
                //else if (itemProperty.Name.Equals("Vertical Extent"))
                //{
                //    //tempNewProperty = 876;
                //    //((IProperty)itemProperty).set_Value(ref tempNewProperty);
                //}
            }

            CommonDialogClass cdc = new WIA.CommonDialogClass();
            try
            {
                imageFile = cdc.ShowTransfer(item, wiaFormatJPEG, false) as ImageFile;
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                imageFile = null;

            }

            while (imageFile != null)
            {

                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                //imageFile.SaveFile(Path.Combine(LocalSavePath, DateTime.Now.Millisecond + ".jpg"));
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    imgs.Add(System.Drawing.Image.FromStream(ms));

                }
                imageFile = null;

                try
                {
                    imageFile = cdc.ShowTransfer(item, wiaFormatJPEG, false) as ImageFile;
                }
                catch (Exception ex)
                {

                    continue;
                    //return imgs;
                    //throw new Exception(ex.Message);
                }
            }
            return imgs;
        }