private void BotonCapturarImagen_Click(object sender, EventArgs e) { try { var WiaDialog = new WIA.CommonDialog(); WIA.ImageFile WiaImage = null; WiaImage = WiaDialog.ShowAcquireImage( WIA.WiaDeviceType.UnspecifiedDeviceType, WIA.WiaImageIntent.ColorIntent, WIA.WiaImageBias.MaximizeQuality, System.Drawing.Imaging.ImageFormat.Jpeg.Guid.ToString("B"), true, false, false); if (WiaImage != null) { var vector = WiaImage.FileData; using (var Recorte = new Entrada.AuxForms.ImagenRecorte()) { Recorte.Imagen = System.Drawing.Image.FromStream(new System.IO.MemoryStream((byte[])vector.get_BinaryData())); if (Recorte.ShowDialog() == System.Windows.Forms.DialogResult.OK) { EntradaImagen.Image = Recorte.Imagen; this.Changed = true; this.ActualizarElemento(); } } } } catch (Exception ex) { Lui.Forms.MessageBox.Show("No se puede conectar con el dispositivo de captura. " + ex.Message, "Error"); } }
/// <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 = string.Empty; 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.ShowAcquireImage(); // 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 ex) { throw new InvalidOperationException(ex.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.WIADPSDOCUMENTHANDLINGSELECT) { documentHandlingSelect = prop; } if (prop.PropertyID == WIA_PROPERTIES.WIADPSDOCUMENTHANDLINGSTATUS) { 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.FEEDREADY) != 0; } } } } return(images); }