/// <summary> /// Use scanner to scan an image (scanner is selected by its unique id). /// </summary> /// <param name="scannerName"></param> /// <returns>Scanned images.</returns> public override List<Image> Scan() { Console.WriteLine("Scan WIA"); 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 == deviceID) { // 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 { foreach (Property prop in item.Properties) { switch (prop.PropertyID) { case 6146: //1 : couleur, 2 : gris, 4 : binaire SetProperty(prop, 2); break; case 6147: //ppp horizontal SetProperty(prop, MainWindow.numerisationDPI); break; case 6148: //ppp vertical SetProperty(prop, MainWindow.numerisationDPI); break; case 6149: //x point where to start scan SetProperty(prop, 0); break; case 6150: //y-point where to start scan SetProperty(prop, 0); break; } } // scan image WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog(); WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, EnvFormatID.wiaFormatTIFF, false); if(image == null) { Console.WriteLine("Numérisation annulé"); return null; } // 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 (System.ArgumentException e) { Console.WriteLine("Le PPP spécifié n'est pas supporté par le scanner"); throw e; } 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; }
/// <summary> /// Gets the infor of WIA devices. /// </summary> /// <returns> Dictionary key and value of properties of wia device</returns> public static Dictionary <string, string> GetDevicesProperty(string deviceid) { WIA.DeviceManager manager = new WIA.DeviceManager(); DeviceInfo device = null; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == deviceid) { device = info; break; } } if (device != null) { Dictionary <string, string> properties = new Dictionary <string, string> { { nameof(device.DeviceID), device.DeviceID }, { nameof(device.Type), device.Type.ToString() } }; foreach (Property p in device.Properties) { properties.Add(p.Name, p.get_Value()); } return(properties); } return(null); }
private static WIA.Device LocalizaDispositio(string scannerId) { WIA.DeviceManager manager = new WIA.DeviceManager(); WIA.Device device = null; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scannerId) { device = info.Connect(); //AcquireNormal(device); Dispositivos[scannerId].Propriedades.Clear(); CarregaPropriedades(device, Dispositivos[scannerId].Propriedades); 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 Mensagens.Add("Não foi possível conectar-se ao o dispositivo especificado\n" + availableDevices); } return(device); }
public static List <string> GetDevicesString() { List <string> devices = new List <string>(); WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (WIA.DeviceInfo info in manager.DeviceInfos) { devices.Add(info.Properties["Name"].get_Value().ToString() + "|" + 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); }
/// <summary> /// Gets the list of available WIA devices. /// </summary> /// <returns></returns> public static IEnumerable <WIADeviceInfo> GetDevices() { List <string> devices = new List <string>(); WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (WIA.DeviceInfo info in manager.DeviceInfos) { // https://msdn.microsoft.com/en-us/library/windows/desktop/ms630313(v=vs.85).aspx yield return(new WIADeviceInfo(info.DeviceID, info.Properties[WIADeviceInfoProp.Manufacturer].get_Value())); } }
public static List <string> GetScannerDevices() { List <string> scannerDevicesList = new List <string>(); WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (WIA.DeviceInfo managerDeviceInfo in manager.DeviceInfos) { scannerDevicesList.Add(managerDeviceInfo.DeviceID); } return(scannerDevicesList); }
public static string GetScannerDevice() { WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type == WiaDeviceType.ScannerDeviceType) { return(info.DeviceID); } } return(null); }
public WIA.Device GetFirstWiaDevice() { WIA.DeviceManager mgr = new WIA.DeviceManager(); WIA.Device retVal = null; foreach (WIA.DeviceInfo info in mgr.DeviceInfos) { if (info.Type == WIA.WiaDeviceType.ScannerDeviceType) { return(mgr.DeviceInfos.OfType <DeviceInfo>().FirstOrDefault(o => o.DeviceID == info.DeviceID).Connect()); } } return(retVal); }
/// <summary> /// Gets the list of available WIA devices. /// </summary> /// <returns></returns> public static List <ScannerDevice> GetDevices() { List <ScannerDevice> devices = new List <ScannerDevice>(); WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (WIA.DeviceInfo info in manager.DeviceInfos) { var device = new ScannerDevice(); device.DeviceId = info.DeviceID; device.DeviceName = info.Properties["Name"].get_Value(); devices.Add(device); } return(devices); }
/// <summary> /// Retorna a lista de dispositivos disponíveis /// </summary> /// <returns></returns> public static IEnumerable <WIADeviceInfo> ListaDispositivos() { List <string> devices = new List <string>(); WIA.DeviceManager manager = new WIA.DeviceManager(); // https://msdn.microsoft.com/en-us/library/windows/desktop/ms630313(v=vs.85).aspx foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.Type == WIA.WiaDeviceType.ScannerDeviceType) { WIADeviceInfo mDevInfo = new WIADeviceInfo(info.DeviceID, info.Properties["Name"].get_Value().ToString()); Dispositivos.Add(info.DeviceID, mDevInfo); yield return(mDevInfo); } } }
/// <summary> /// Gets the list of available WIA devices. /// </summary> /// <returns></returns> public static List <Tuple <string, string> > GetDevices() { List <Tuple <string, string> > devices = new List <Tuple <string, string> >(); WIA.DeviceManager manager = new WIA.DeviceManager(); foreach (WIA.DeviceInfo info in manager.DeviceInfos) { Device dev = info.Connect( ); devices.Add(Tuple.Create( info.DeviceID, GetDeviceProperty(dev, (int)WiaProperty.Name).ToString())); } return(devices); }
public static WIA.Device ConnectToScanner(string scannerDeviceID) { WIA.DeviceManager manager = new WIA.DeviceManager(); WIA.Device device = null; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scannerDeviceID) { device = info.Connect(); break; } } return(device); }
/// <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); }
public static List <string> GetDevices() { string deviceName = ""; List <string> devices = new List <string>(); 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") { deviceName = ((WIA.IProperty)p).get_Value().ToString(); devices.Add(deviceName); } } } } 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, "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}", false); //wiaItem.Transfer("{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}") as WIA.ImageFile; return(imageFile.FileData.get_BinaryData()); } catch (Exception e) { throw; } }
/// <summary> /// Use scanner to scan an image (scanner is selected by its unique id). /// </summary> /// <param name="scannerName"></param> /// <returns>Scanned images path.</returns> public static List <string> Scan(string scannerId, DoWorkEventArgs e) { List <string> images = new List <string>(); bool hasMorePages = true; ITempFilesResource <ImageFile> tempFiles = TempFilesScannerResource <ImageFile> .DefaultInstance(); while (hasMorePages && !e.Cancel) { // 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 = tempFiles.SaveFile(image); image = null; // add file to output list images.Add(fileName); } catch (System.Runtime.InteropServices.COMException exc) { Debug.WriteLine(exc.Message); } catch (Exception exc) { Debug.WriteLine(exc.Message); } 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); } } } } if (!e.Cancel) { return(images); } else { foreach (var image in images) { tempFiles.DeleteFile(image); } images.Clear(); return(images); } }
/// <summary> /// Use scanner to scan an image (scanner is selected by its unique id). /// </summary> /// <param name="scannerName"></param> /// <returns>Scanned images.</returns> public List <Image> Scan(string scannerId, int pages) { //List<Image> images = new List<Image>(); bool hasMorePages = true; int numbrPages = pages; // 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); } SetWIAProperty(device.Properties, WIA_DEVICE_PROPERTY_PAGES_ID, 1); item = device.Items[1] as WIA.Item; int dpi = 300; int inch = 254; double dpiMM = dpi / inch; double widthFormat = 210; double heightFormat = 297; int widthPixel = (int)(widthFormat * dpiMM); int heightPixel = (int)(heightFormat * dpiMM); AdjustScannerSettings(item, dpi, 0, 0, widthPixel, heightPixel, 0, 0, 2); while (hasMorePages) { Thread thread = new Thread(goScan); thread.Start(); thread.Join(); /* * 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); * } * } * } */ numbrPages -= 1; if (numbrPages > 0) { hasMorePages = true; } else { hasMorePages = false; } } return(images); }
/// <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 <string> Scan(ScanSettings settings) { List <string> images = new List <string>(); 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 == settings.DeviceId) { // 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(); WIAScanner.AdjustScannerSettings(item, settings.WIA_Intent); WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false); var imageBytes = (byte[])image.FileData.get_BinaryData(); images.Add(Convert.ToBase64String(imageBytes)); } 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); }
/// <summary> /// Gets the list of available WIA devices. /// </summary> /// <returns></returns> public static List<string> GetDevicesId() { 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> /// 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); } // Set color intent to greyscale if user requested it if (clrMode == Utils.ColorMode.Greyscale) { Property prop = device.Items[1].Properties.get_Item("6146"); prop.set_Value(2); //4 is Black-white,gray is 2, color 1 (Color Intent) } 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 = Utils.GetNextFileName(scannedDocumentsPath + "\\NumérisationSansTitre.bmp"); //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 static List <Bitmap> Scan(string scannername) { List <Bitmap> images = new List <Bitmap>(); bool hasMorePages = true; while (hasMorePages) { WIA.DeviceManager manager = new WIA.DeviceManager(); WIA.Device device = null; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.Properties["Name"].get_Value().ToString() == scannername) { device = info.Connect(); break; } } if (device == null) { string availableDevices = ""; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { availableDevices += info.Properties["Name"].get_Value().ToString() + "\n"; } throw new Exception("Указанное устройство не найдено. Доступные устройства: " + availableDevices); } WIA.Item item = device.Items[1] as WIA.Item; try { WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog(); AdjustScannerSettings(item, 100, 0, 0, 850, 1170, 0, 0, true); WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false); //темповый файл string fileName = Path.GetTempFileName(); File.Delete(fileName); image.SaveFile(fileName); image = null; images.Add(new Bitmap(fileName)); } catch (Exception exc) { throw exc; } finally { item = null; 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; } } hasMorePages = false; if (documentHandlingSelect != null) { 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 static List<string> GetDevices() { string deviceName = ""; List<string> devices = new List<string>(); 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") { deviceName = ((WIA.IProperty)p).get_Value().ToString(); devices.Add(deviceName); } } } } return devices; }
/// <summary> /// /// </summary> /// <param name="scannerId"></param> /// <param name="qualidade"></param> /// <param name="tamanho"></param> /// <param name="tipo"></param> /// <returns></returns> private static List <KeyValuePair <string, Image> > DigitalizaItensEspecifico(string scannerId, WIAScanQuality qualidade, WIAPageSize tamanho, TipoLeituraDocumento tipo) { List <KeyValuePair <string, Image> > images = new List <KeyValuePair <string, Image> >(); //Dictionary<string,Image> images = new Dictionary<string,Image>(); bool hasMorePages = true; WIA.Item item; Mensagens = new HashSet <string>(); // 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) { device = info.Connect(); //AcquireNormal(device); Dispositivos[scannerId].Propriedades.Clear(); CarregaPropriedades(device, Dispositivos[scannerId].Propriedades); 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 Mensagens.Add("Não foi possível conectar-se ao o dispositivo especificado\n" + availableDevices); } ConfiguraTipoScan(ref device, tipo); item = device.Items[1] as WIA.Item; AjustaPropriedadesDispositivo(item, 0, 0, 0, 0, 1, qualidade, tamanho); WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog(); while (hasMorePages) { try { //Some scanner need WIA_DPS_PAGES to be set to 1, otherwise all pages are acquired but only one is returned as ImageFile GravaPropriedade(ref device, DEVICE_PROPERTY_PAGES_ID, 1); //Scan image WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, WIA.FormatID.wiaFormatPNG, false); if (image != null) { // save to file string fileName = SalvarPNG(image); images.Add(new KeyValuePair <string, Image>(fileName, Image.FromFile(fileName))); } //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); } } } catch (Exception exc) { int error = CodigoErroWIA(exc); Mensagens.Add(DescricaoErro(exc)); if (error == WIA_ERROR_PAPER_EMPTY) { hasMorePages = false; } if (error == 0) { throw exc; } } } device = null; return(images); }
protected void btnEscanear_Click(object sender, EventArgs e) { WIA.CommonDialog Dialog1 = new WIA.CommonDialog(); WIA.DeviceManager DeviceManager1 = new WIA.DeviceManager(); WIA.Device Scanner = null; Scanner = Dialog1.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, true); WIA.Item Item1 = Scanner.Items[1]; WIA.ImageFile Imagen = new WIA.ImageFile(); Imagen = (WIA.ImageFile)Item1.Transfer("{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"); string DestImagePath = @"~\imagenes\temporal\Scan.png"; File.Delete(DestImagePath); Imagen.SaveFile(DestImagePath); Image1.ImageUrl = @"~\imagenes\temporal\Scan.png"; string nombreArchivo = "Scan.png"; selectedTipo = dropTipoDocumento.SelectedValue.ToString(); ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(0); Bitmap image1 = new Bitmap(Server.MapPath(@"imagenes").ToString() + @"\" + nombreArchivo); Bitmap image2 = null; if (dropTipoDocumento.SelectedValue == "acta") { image2 = new Bitmap(Server.MapPath(@"imagenes").ToString() + @"\templer.jpg"); } else if (dropTipoDocumento.SelectedValue == "curp") { image2 = new Bitmap(Server.MapPath(@"imagenes").ToString() + @"\curptemp.jpg"); } else { } TemplateMatch[] matchings = tm.ProcessImage(image1, image2, new Rectangle(new Point(0, 0), new Size(250, 250))); if (matchings[0].Similarity > 0.8f) { int idDocumentos = Convert.ToInt32(datos.SelectValor("select count(*) from Documentos")) + 1; if (dropCategoria.SelectedValue == "InfoPersonal") { // Son Similares this.Response.Write("<script language='JavaScript'>window.alert('la imagen se subio correctamente')</script>"); Imagen.SaveFile(Server.MapPath(@"imagenes").ToString() + @"\InfoPersonal\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png"); string idEmpleado = datos.SelectValor("select idEmpleado from Empleado where correoElectronico='" + Session["userName"] + "'"); datos.Comand("insert into Documentos values(" + idDocumentos + "," + idEmpleado + ",'" + selectedTipo + "','" + dropCategoria.SelectedValue + "','" + Server.MapPath(@"imagenes").ToString() + @"\InfoPersonal\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png" + "')"); } else if (dropCategoria.SelectedValue == "InfoAcademica") { this.Response.Write("<script language='JavaScript'>window.alert('la imagen se subio correctamente')</script>"); Imagen.SaveFile(Server.MapPath(@"imagenes").ToString() + @"\InfoAcademica\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png"); string idEmpleado = datos.SelectValor("select idEmpleado from Empleado where correoElectronico='" + Session["userName"] + "'"); datos.Comand("insert into Documentos values(" + idDocumentos + "," + idEmpleado + ",'" + selectedTipo + "','" + dropCategoria.SelectedValue + "','" + Server.MapPath(@"imagenes").ToString() + @"\InfoAcademica\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png" + "')"); } else if (dropCategoria.SelectedValue == "InfoLaboral") { this.Response.Write("<script language='JavaScript'>window.alert('la imagen se subio correctamente')</script>"); Imagen.SaveFile(Server.MapPath(@"imagenes").ToString() + @"\InfoLaboral\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png"); string idEmpleado = datos.SelectValor("select idEmpleado from Empleado where correoElectronico='" + Session["userName"] + "'"); datos.Comand("insert into Documentos values(" + idDocumentos + "," + idEmpleado + ",'" + selectedTipo + "','" + dropCategoria.SelectedValue + "','" + Server.MapPath(@"imagenes").ToString() + @"\InfoLaboral\" + selectedTipo + "_" + Session["userName"] + "_" + "Scan.png" + "')"); } } else { //No son similares this.Response.Write("<script language='JavaScript'>window.alert('la imagen que subiste no coincide con el tipo de archivo que seleccionaste')</script>"); Image1.ImageUrl = ""; } }
public AcquireManager() { this.wiaDialog = new WIA.CommonDialog(); deviceManager = new DeviceManager(); }
/// <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; //AdjustScannerSettings(scannnerItem, (int)nudRes.Value, 0, 0, (int)nudWidth.Value, (int)nudHeight.Value, 0, 0, cmbCMIndex); AdjustScannerSettings(item, 2); 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); }
/// <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, int pages, WIAScanQuality quality, WIAPageSize pageSize, DocumentSource source) { List <Image> images = new List <Image>(); bool hasMorePages = true; int numbrPages = pages; 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); } SetWIAProperty(device.Properties, WIA_DEVICE_PROPERTY_PAGES_ID, 1); SetWIAProperty(device.Properties, WIA_DEVICE_SOURCE_SELECT_ID, source); WIA.Item item = device.Items[1] as WIA.Item; // adjust the scan settings int dpi; int width_pixels; int height_pixels; switch (quality) { case WIAScanQuality.Final: dpi = 300; break; default: throw new Exception("Unknown WIAScanQuality: " + quality.ToString()); } switch (pageSize) { case WIAPageSize.A4: width_pixels = (int)(8.3f * dpi); height_pixels = (int)(11.7f * dpi); break; case WIAPageSize.Letter: width_pixels = (int)(8.5f * dpi); height_pixels = (int)(11f * dpi); break; case WIAPageSize.Legal: width_pixels = (int)(8.5f * dpi); height_pixels = (int)(14f * dpi); break; default: throw new Exception("Unknown WIAPageSize: " + pageSize.ToString()); } AdjustScannerSettings(item, dpi, 0, 0, width_pixels, height_pixels, 0, 0, 1); 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); } } } numbrPages -= 1; if (numbrPages > 0) { hasMorePages = true; } else { hasMorePages = false; } } return(images); }
/// <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 = 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, "{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; 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; } } } }
public void Initialize(string messages) { wiaDevManager = new WIA.DeviceManagerClass(); SetNotification(0, "Wia Manager has been initialized"); }
public static List<System.Drawing.Image> preScan(string scannerId) { List<System.Drawing.Image> images = new List<System.Drawing.Image>(); bool hasMorePages = true; while (hasMorePages) { WIA.DeviceManager manager = new WIA.DeviceManager(); WIA.Device device = null; foreach (WIA.DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scannerId) { device = info.Connect(); break; } } if (device == null) { 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 { 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(System.Drawing.Image.FromFile(fileName)); } catch (Exception exc) { throw exc; } finally { item = null; } 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; }