private Device CreateDeviceManager() { DeviceManager manager = new DeviceManagerClass(); Device wiaDev = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == m_DeviceID) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner try { Thread.Sleep(500); wiaDev = info.Connect(); } catch (Exception) { Thread.Sleep(500); //throw; } break; } } return(wiaDev); }
public void TestCameraProvider() { var provider = new TestService(); var importService = new ImportService(provider, new FileSystemService(), new LogService(), new DateTimeService()); var deviceManagerClass = new DeviceManagerClass(); var deviceInfo = deviceManagerClass.DeviceInfos.Cast<DeviceInfo>().FirstOrDefault(p => string.Equals(p.DeviceID, provider.GetSettings().DeviceId, StringComparison.OrdinalIgnoreCase)); var device = deviceInfo.Connect(); importService.Import(new CameraPhotoProvider(device), null, null); }
public static List<ScanDevice> GetScanDeviceList() { DeviceManager manager = new DeviceManagerClass(); var devices = new List<ScanDevice>(); foreach (DeviceInfo info in manager.DeviceInfos) { Device device = info.Connect(); devices.Add(new ScanDevice(info.DeviceID, GetDeviceName(device))); } return devices; }
/// <summary> /// Ermittelt das erste verfügbare WIA-Gerät /// </summary> /// <returns>das erste verfügbare WIA-Gerät</returns> public static Device GetFirstScannerDevice() { DeviceManager deviceManager = new DeviceManagerClass(); foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos) { if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType) { return(deviceInfo.Connect()); } } throw new ArgumentException("Keinen Scanner gefunden."); }
public static string GetDeviceName(string deviceID) { DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == deviceID) { Device device = info.Connect(); return GetDeviceName(device); } } throw new DeviceNotFoundException(); }
/// <summary> /// Ermittelt die Referenz auf ein WIA-Gerät /// </summary> /// <param name="deviceID">ID des Geräts</param> /// <returns>Referenz auf das WIA-Gerät mit der angegebenen ID</returns> public static Device FromDeviceId(string deviceID) { DeviceManager deviceManager = new DeviceManagerClass(); foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos) { if (deviceInfo.DeviceID == deviceID) { return(deviceInfo.Connect()); } } throw new ArgumentException("Kein Device mit der ID '" + deviceID + "' gefunden."); }
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 CScannerAPI(CScanSettings settings) { this.settings = settings; DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == settings.DeviceID) { device = info.Connect(); return; } } throw new Exceptions.EScannerNotFound(); }
public CWIAAPI(string DeviceID) { settings = new CScanSettings(); DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == DeviceID) { device = info.Connect(); return; } } throw new Exceptions.EScannerNotFound(); }
public ADFScan(string deviceID) { _deviceID = deviceID; DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == _deviceID) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner _WiaDev = info.Connect(); break; } } }
private void ButtonLoadCameraClick(object sender, EventArgs e) { var deviceManager = new DeviceManagerClass(); if (deviceManager.DeviceInfos.Cast<DeviceInfo>().All(p => p.Type != WiaDeviceType.CameraDeviceType)) { MessageBox.Show(CanNotFindAnyDevice); return; } var commonDialog = new CommonDialogClass(); var device = commonDialog.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true); if (device == null) { return; } this.Import(new CameraPhotoProvider(device)); }
private void ButtonDeviceClick(object sender, System.EventArgs e) { DeviceManager deviceManager = new DeviceManagerClass(); if (deviceManager.DeviceInfos.Cast<DeviceInfo>().All(p => p.Type != WiaDeviceType.CameraDeviceType)) { MessageBox.Show(CanNotFindAnyDevice); return; } var commonDialog = new CommonDialogClass(); var device = commonDialog.ShowSelectDevice(WiaDeviceType.CameraDeviceType, true); if (device == null) { return; } this.settings.DeviceId = device.DeviceID; this.UpdateControls(); }
public static Device GetDevice(ScanDevice scanDevice) { DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scanDevice.ID) { try { return info.Connect(); } catch (COMException e) { ThrowDeviceError(e); } } } throw new DeviceNotFoundException(); }
/// <summary> /// 文件扫描函数 /// </summary> /// <param name="byteImage">输出图片二进制字节流</param> /// <param name="x">扫描起始点x轴</param> /// <param name="y">扫描起始点y轴</param> /// <param name="w">扫描范围宽度</param> /// <param name="h">扫描范围高度</param> /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns> public bool ScanFile(out byte[] byteImage, int x, int y, int w, int h) { byteImage = null; DeviceManager manager = new DeviceManagerClass(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type != WiaDeviceType.ScannerDeviceType) { continue; } device = info.Connect(); break; } Item item = device.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[]; using (System.IO.MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp"; Image.FromStream(ms).Save(strCurpath, ImageFormat.Bmp); Image img = Clip((Bitmap)Image.FromStream(ms), x, y, w, h); img.Save(strCurpath, ImageFormat.Bmp); byteImage = System.IO.File.ReadAllBytes(strCurpath); return(true); } } else { return(false); } }
public static Device GetDevice(ScanDevice scanDevice) { DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scanDevice.ID) { try { return info.Connect(); } catch (COMException e) { if ((uint)e.ErrorCode == Errors.OFFLINE) { throw new DeviceOfflineException(); } throw new ScanDriverUnknownException(e); } } } throw new DeviceNotFoundException(); }
public static Device GetDevice(ScanDevice scanDevice) { DeviceManager manager = new DeviceManagerClass(); foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == scanDevice.ID) { try { return(info.Connect()); } catch (COMException e) { if ((uint)e.ErrorCode == Errors.OFFLINE) { throw new DeviceOfflineException(); } throw new ScanDriverUnknownException(e); } } } throw new DeviceNotFoundException(); }
/// <summary> /// 自动扫描文件 /// </summary> /// <param name="byteImage">输出图片二进制字节流</param> /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns> public bool AutoScanFile(out byte[] byteImage) { byteImage = null; int xLeft = 0; int xRight = 0; int yTotp=0; int yBotom=0; DeviceManager manager = new DeviceManagerClass(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type != WiaDeviceType.ScannerDeviceType) continue; device = info.Connect(); break; } Item item = device.Items[1]; CommonDialogClass cdc = new WIA.CommonDialogClass(); ImageFile imageFile = cdc.ShowTransfer(item, "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}", true) as ImageFile; System.IO.MemoryStream ms = new MemoryStream(); if (imageFile != null) { var buffer = imageFile.FileData.get_BinaryData() as byte[]; ms.Write(buffer, 0, buffer.Length); } else { return false; } Color c1 = new Color(); Color c2 = new Color(); Color c3 = new Color(); Color c4 = new Color(); Color c5 = new Color(); Color c6 = new Color(); Color c7 = new Color(); Color c8 = new Color(); Color c9 = new Color(); int rr, r1, r2, r3, r4, r5, r6, r7, r8, r9, fxr, i, j; // int g1, g2, g3, g4, fxg, fyg, b1, b2, b3, b4, fxb, fyb; Bitmap imgTem = (Bitmap)Bitmap.FromStream(ms); for (i = 1; i < imgTem.Width - 2; i++) { for (j = 1; j < imgTem.Height - 2; j++) { c1 = imgTem.GetPixel(i, j - 1); c2 = imgTem.GetPixel(i - 1, j); c3 = imgTem.GetPixel(i, j); c4 = imgTem.GetPixel(i + 1, j); c5 = imgTem.GetPixel(i, j + 1); c6 = imgTem.GetPixel(i - 1, j - 1); c7 = imgTem.GetPixel(i - 1, j + 1); c8 = imgTem.GetPixel(i + 1, j - 1); c9 = imgTem.GetPixel(i + 1, j + 1); r1 = c1.R; r2 = c2.R; r3 = c3.R; r4 = c4.R; r5 = c5.R; r6 = c6.R; r7 = c7.R; r8 = c8.R; r9 = c9.R; fxr = 8 * r3 - r1 - r2 - r4 - r5 - r6 - r7 - r8 - r9; // fyr = r6 + 2 * r1 + r8 - r7 - 2 * r5 - r9; rr = Math.Abs(fxr); if (rr < 0) rr = 0; if (rr > 255) rr = 255; Color cc = Color.FromArgb(rr, rr, rr); imgTem.SetPixel(i, j, cc); } } bool isc = false; Color pixel; //污点熟练 int maxc = 5; #region 扫描左空白 for (int x = 1; x < imgTem.Width - 2; x++) { int c = 0; for (int y = 1; y < imgTem.Height - 2; y++) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { isc = true; } } if (isc == false) { xLeft = x; break; } } #endregion #region 扫描右空白 for (int x = imgTem.Width - 2; x > 2; x--) { int c = 0; for (int y = imgTem.Height - 2; y > 2; y--) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { isc = true; } } if (isc == false) { xRight = x; break; } } #endregion #region 扫描底部空白 for (int y = imgTem.Height - 2; y > 2; y--) { int c = 0; for (int x = imgTem.Width - 2; x > 2; x--) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { //imgTem = Clip(imgTem, 0, 0, x, imgTem.Height); //pictureimgTem.Image = imgTem; //pictureimgTem.Image = imgTem; isc = true; } } if (isc == false) { yBotom = y; break; } } #endregion #region 扫描上部空白 for (int y = 1; y < imgTem.Height - 2; y++) { int c = 0; for (int x = 1; x < imgTem.Width - 2; x++) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { isc = true; } } if (isc == false) { yTotp = y; break; } } #endregion //剪切图片 imgTem = (Bitmap)Bitmap.FromStream(ms); imgTem = Clip(imgTem, xLeft - 5, yTotp - 5, xRight - xLeft + 5, yBotom - yTotp + 5); string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp"; imgTem.Save(strCurpath, ImageFormat.Bmp); byteImage = System.IO.File.ReadAllBytes(strCurpath); return true; }
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(); }
private void Scan(ScanColor clr, int dpi) { // Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(@"CLSID\{D2923B86-15F1-46FF-A19A-DE825F919576}\SupportedExtension\.jpg"); //string jpegGuid = jpegKey.GetValue("FormatGUID") as string; CommonDialogClass commonDialogClass = new CommonDialogClass(); Device device = commonDialogClass.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false); if (device != null) { string deviceID = device.DeviceID; WIA.CommonDialog commonDialog = new CommonDialogClass(); bool flag = true; int num = 0; int num2 = 0; while (flag) { DeviceManager deviceManager = new DeviceManagerClass(); Device device2 = null; foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos) { if (deviceInfo.DeviceID == deviceID) { WIA.Properties properties = deviceInfo.Properties; device2 = deviceInfo.Connect(); break; } } Item item = device2.Items[1]; object obj = (int)clr; object obj2 = "6146"; setItem(item, obj2, obj); object obj3 = dpi; object obj4 = "6147"; setItem(item, obj4, obj3); object obj5 = dpi; object obj6 = "6148"; setItem(item, obj6, obj5); try { ImageFile imageFile = (ImageFile)commonDialog.ShowTransfer(item, "{00000000-0000-0000-0000-000000000000}", false); string tempFileName = Path.GetTempFileName(); if (File.Exists(tempFileName)) { File.Delete(tempFileName); } imageFile.SaveFile(tempFileName); Image img = Image.FromFile(tempFileName); EventHandler <WiaImageEventArgs> scanning = this.Scanning; if (scanning != null) { scanning(this, new WiaImageEventArgs(img)); } num2++; } catch (Exception ex) { throw ex; } finally { Property property = null; Property property2 = null; foreach (Property property3 in device2.Properties) { if ((long)property3.PropertyID == 3088L) { property = property3; } if ((long)property3.PropertyID == 3087L) { property2 = property3; } } flag = false; if (property != null) { if ((Convert.ToUInt32(property.get_Value()) & 1u) != 0u) { flag = ((Convert.ToUInt32(property2.get_Value()) & 1u) != 0u); } } num++; } } EventHandler scanComplete = this.ScanComplete; if (scanComplete != null) { scanComplete(this, EventArgs.Empty); } } }
private void btnAutoScan_Click(object sender, EventArgs e) { DeviceManager manager = new DeviceManagerClass(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type != WiaDeviceType.ScannerDeviceType) { continue; } device = info.Connect(); break; } Item item = device.Items[1]; //string s = ""; //for (int i = 1; i < item.Properties.Count - 1; i++) //{ // object o = (int)i; // Property p = item.Properties.get_Item(ref o); // s += p.Name + "----" + p.PropertyID + "----" + Convert.ToString(p.get_Value()) + "\r\n"; //} int dpi = (int)numericUpDown1.Value; ScanColor color = (ScanColor)this._colors[this.comboBox1.SelectedIndex]; object obj = (int)color; object obj2 = "6146";//WiaImageIntent setItem(item, obj2, obj); object obj3 = dpi; object obj4 = "6147"; setItem(item, obj4, obj3); object obj5 = dpi; object obj6 = "6148"; setItem(item, obj6, obj5); setItem(item, "6151", 800); setItem(item, "6152", 100 * 11); CommonDialogClass cdc = new WIA.CommonDialogClass(); ImageFile imageFile = cdc.ShowTransfer(item, "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}", true) as ImageFile; if (imageFile != null) { var buffer = imageFile.FileData.get_BinaryData() as byte[]; using (MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); pictureBox1.Image = Image.FromStream(ms); } } }
private void DoScan() { WIA.Item item = default(WIA.Item); WIA.ImageFile Img = default(WIA.ImageFile); WIA.CommonDialog WiaCommonDialog = new CommonDialogClass(); bool hasMorePages = true; int x = 0; int numPages = 0; string ImgMain = null; string ImgMainName = null; try { if (treealfresco.SelectedNode == null) { MessageBox.Show("Please select a Space to store your data into", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ResultSetRowNode node = (ResultSetRowNode)treealfresco.SelectedNode.Tag; LocationUuid = node.id; LocationName = treealfresco.SelectedNode.Text; } catch (Exception ex) { MessageBox.Show(ex.Message); } while (hasMorePages) { //Create DeviceManager DeviceManager manager = new DeviceManagerClass(); Device WiaDev = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == this.DeviceID) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner WiaDev = info.Connect(); break; // TODO: might not be correct. Was : Exit For } } //Start Scan Img = null; item = WiaDev.Items[1] as WIA.Item; try { Img = (ImageFile)WiaCommonDialog.ShowTransfer(item, wiaFormatTIFF, false); //process image: //one would do image processing here //Save to file string jpegGuid = null; //retrieves jpegKey from registry, used in saving JPEG Microsoft.Win32.RegistryKey jpegKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey("CLSID\\{D2923B86-15F1-46FF-A19A-DE825F919576}\\SupportedExtension\\.jpg"); jpegGuid = (string)jpegKey.GetValue("FormatGUID"); //loops through available formats for the captured item, looking for the JPG format foreach (string format in item.Formats) { if ((format == jpegGuid)) { //transfers image to an imagefile object Img = (WIA.ImageFile)item.Transfer(WIA.FormatID.wiaFormatTIFF); int Counter = 0; //counter in loop appended to filename bool LoopAgain = true; //searches directory, gets next available picture name while (!(LoopAgain == false)) { string File = SavePath + "\\"; File += txtPrefix.Text; File += Counter; File += ".tiff"; string Filename = txtPrefix.Text; Filename += Counter; Filename += ".tiff"; if (System.IO.File.Exists(Filename)) { //file exists, delete it System.IO.File.Delete(Filename); } if (numPages == 0) { Img.SaveFile(Filename); } ImgMain = File; ImgMainName = Filename; if (numPages > 0) { int y = Counter; y = Counter - 1; ImgMain = SavePath + "\\"; ImgMain += txtPrefix.Text; ImgMain += y; ImgMain += ".tiff"; ImgMainName = txtPrefix.Text; ImgMainName += y; ImgMainName += ".tiff"; //createtiff(ImgMain, File); } numPages += 1; Counter = Counter + 1; } } } } catch (Exception ex) { MessageBox.Show("Error: " + 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 WiaDev.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; UploadNow(ImgMainName, ImgMain); MessageBox.Show(ImgMain + " uploaded"); //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); } } x += 1; } } }
/// <summary> /// 扫描文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnImageMagnify_Click(object sender, EventArgs e) { try { DeviceManager manager = new DeviceManagerClass(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type != WiaDeviceType.ScannerDeviceType) { continue; } device = info.Connect(); break; } Item item = device.Items[1]; CommonDialogClass cdc = new WIA.CommonDialogClass(); ImageFile imageFile = null; imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, WIA.WiaImageIntent.TextIntent, WIA.WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", true, true, false); if (imageFile != null) { var buffer = imageFile.FileData.get_BinaryData() as byte[]; using (MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); string filePath = Server.MapPath("~/") + AttachPath; ///文件夹 if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string name = "\\"; var menu = BLL.SysMenuService.GetSysMenuByMenuId(this.MenuId); if (menu != null) { name += menu.MenuName; } name += Funs.GetNewFileName() + ".jpg"; string url = filePath + name; if (!string.IsNullOrEmpty(url)) { using (FileStream fs = new FileStream(url, FileMode.Create, FileAccess.Write)) { ms.WriteTo(fs); string attachUrl = AttachPath + name; if (!string.IsNullOrEmpty(attachUrl)) { attachUrl = attachUrl.Replace('/', '\\'); } string oldSrouce = string.Empty; string FullPath = string.Empty; Model.AttachFile att = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == this.ToKeyId); if (att != null && !string.IsNullOrEmpty(att.AttachUrl)) { FullPath = att.AttachUrl + "," + attachUrl; oldSrouce = att.AttachSource; } else { FullPath = attachUrl; } string source = BLL.UploadFileService.GetSourceByAttachUrl(attachUrl, buffer.Length, oldSrouce); //this.SaveData(source, FullPath); ///保存方法 Session[sessionName] = JArray.Parse(source); } this.BindGrid(); ShowNotify("扫描完成!", MessageBoxIcon.Success); } } } } catch { ShowNotify("请检查扫描仪是否连接正确!", MessageBoxIcon.Warning); } }
void Scan(ScanColor clr, int dpi) { if (_WiaDev == null) { return; } WIA.CommonDialog WiaCommonDialog = new CommonDialogClass(); bool hasMorePages = true; int x = 0; int numPages = 0; //determine if there are any more pages waiting Property documentHandlingSelect = null; Property documentHandlingStatus = null; foreach (Property prop in _WiaDev.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 != true) { hasMorePages = true; } while (hasMorePages) { //Create DeviceManager DeviceManager manager = new DeviceManagerClass(); _WiaDev = null; //Device WiaDev = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == _deviceID) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner _WiaDev = info.Connect(); break; } } //Start Scan WIA.ImageFile img = null; WIA.Item Item = _WiaDev.Items[1] as WIA.Item; //set properties //BIG SNAG!! if you call WiaDev.Items[1] apprently it erases the item from memory so you cant call it again Item.Properties["6146"].set_Value((int)clr);//Item MUST be stored in a variable THEN the properties must be set. Item.Properties["6147"].set_Value(dpi); Item.Properties["6148"].set_Value(dpi); try {//WATCH OUT THE FORMAT HERE DOES NOT MAKE A DIFFERENCE... .net will load it as a BITMAP! img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, WIA.FormatID.wiaFormatJPEG, false); //process image: //Save to file and open as .net IMAGE string varImageFileName = Path.GetTempFileName(); if (File.Exists(varImageFileName)) { //file exists, delete it File.Delete(varImageFileName); } img.SaveFile(varImageFileName); Image ret = Image.FromFile(varImageFileName); EventHandler <WiaImageEventArgs> temp = Scanning; if (temp != null) { temp(this, new WiaImageEventArgs(ret)); } numPages++; img = null; } catch (Exception ex) { //throw ex; } finally { Item = null; ////determine if there are any more pages waiting //Property documentHandlingSelect = null; //Property documentHandlingStatus = null; foreach (Property prop in _WiaDev.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); } } x++; } } EventHandler tempCom = ScanComplete; if (tempCom != null) { tempCom(this, EventArgs.Empty); } }
void Scan(ScanColor clr, int dpi) { string deviceid; //Choose Scanner CommonDialogClass class1 = new CommonDialogClass(); Device d = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false); if (d != null) { deviceid = d.DeviceID; } else { //no scanner chosen return; } WIA.CommonDialog WiaCommonDialog = new CommonDialogClass(); bool hasMorePages = true; int x = 0; int numPages = 0; while (hasMorePages) { //Create DeviceManager DeviceManager manager = new DeviceManagerClass(); Device WiaDev = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == deviceid) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner WiaDev = info.Connect(); break; } } //Start Scan WIA.ImageFile img = null; WIA.Item Item = WiaDev.Items[1] as WIA.Item; //set properties //BIG SNAG!! if you call WiaDev.Items[1] apprently it erases the item from memory so you cant call it again Item.Properties["6146"].set_Value((int)clr);//Item MUST be stored in a variable THEN the properties must be set. Item.Properties["6147"].set_Value(dpi); Item.Properties["6148"].set_Value(dpi); try {//WATCH OUT THE FORMAT HERE DOES NOT MAKE A DIFFERENCE... .net will load it as a BITMAP! img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, WIA.FormatID.wiaFormatJPEG, false); //process image: //Save to file and open as .net IMAGE string varImageFileName = Path.GetTempFileName(); if (File.Exists(varImageFileName)) { //file exists, delete it File.Delete(varImageFileName); } img.SaveFile(varImageFileName); Image ret = Image.FromFile(varImageFileName); EventHandler<WiaImageEventArgs> temp = Scanning; if (temp != null) { temp(this, new WiaImageEventArgs(ret)); } numPages++; img = null; } catch (Exception ex) { throw ex; } finally { Item = null; //determine if there are any more pages waiting Property documentHandlingSelect = null; Property documentHandlingStatus = null; foreach (Property prop in WiaDev.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); } } x++; } } EventHandler tempCom = ScanComplete; if (tempCom != null) { tempCom(this, EventArgs.Empty); } }
public ArrayList ADFScan() { ArrayList dataArray = new ArrayList(); //Choose Scanner CommonDialogClass class1 = new CommonDialogClass(); // //class1.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, false, false); // Device d = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false); if (d != null) { this.DeviceID = d.DeviceID; } else { //no scanner chosen return(new ArrayList()); } WIA.CommonDialog WiaCommonDialog = new CommonDialogClass(); bool hasMorePages = true; int x = 0; int numPages = 0; while (hasMorePages) { //Create DeviceManager DeviceManager manager = new DeviceManagerClass(); Device WiaDev = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.DeviceID == this.DeviceID) { WIA.Properties infoprop = null; infoprop = info.Properties; //connect to scanner WiaDev = info.Connect(); break; } } //Start Scan WIA.ImageFile img = null; WIA.Item Item = WiaDev.Items[1] as WIA.Item; try { img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, wiaFormatJPEG, false); //process image: //one would do image processing here // dataArray.Add(img); //Save to file //string varImageFileName = "c:\\test" + x.ToString() + ".jpg"; // if (File.Exists(varImageFileName)) // { // //file exists, delete it // File.Delete(varImageFileName); // } // img.SaveFile(varImageFileName); numPages++; img = null; } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); } finally { Item = null; //determine if there are any more pages waiting Property documentHandlingSelect = null; Property documentHandlingStatus = null; foreach (Property prop in WiaDev.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); } } x++; } } return(dataArray); }
/// <summary> /// 文件扫描函数 /// </summary> /// <param name="byteImage">输出图片二进制字节流</param> /// <param name="x">扫描起始点x轴</param> /// <param name="y">扫描起始点y轴</param> /// <param name="w">扫描范围宽度</param> /// <param name="h">扫描范围高度</param> /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns> public bool ScanFile(out byte[] byteImage, int x, int y, int w, int h) { byteImage = null; DeviceManager manager = new DeviceManagerClass(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type != WiaDeviceType.ScannerDeviceType) continue; device = info.Connect(); break; } Item item = device.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[]; using (System.IO.MemoryStream ms = new MemoryStream()) { ms.Write(buffer, 0, buffer.Length); string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp"; Image.FromStream(ms).Save(strCurpath, ImageFormat.Bmp); Image img = Clip((Bitmap)Image.FromStream(ms), x, y, w, h); img.Save(strCurpath, ImageFormat.Bmp); byteImage = System.IO.File.ReadAllBytes(strCurpath); return true; } } else { return false; } }
/// <summary> /// Initializes a new instance of the <see cref="CameraService"/> class. /// </summary> public CameraService() { this.deviceManager = new DeviceManagerClass(); this.deviceManager.OnEvent += this.DeviceManagerOnEvent; }
/// <summary> /// 自动扫描文件 /// </summary> /// <param name="byteImage">输出图片二进制字节流</param> /// <returns>执行结构 true表示扫描成功 fale表示扫描失败</returns> public bool AutoScanFile(out byte[] byteImage) { byteImage = null; int xLeft = 0; int xRight = 0; int yTotp = 0; int yBotom = 0; DeviceManager manager = new DeviceManagerClass(); Device device = null; foreach (DeviceInfo info in manager.DeviceInfos) { if (info.Type != WiaDeviceType.ScannerDeviceType) { continue; } device = info.Connect(); break; } Item item = device.Items[1]; CommonDialogClass cdc = new WIA.CommonDialogClass(); ImageFile imageFile = cdc.ShowTransfer(item, "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}", true) as ImageFile; System.IO.MemoryStream ms = new MemoryStream(); if (imageFile != null) { var buffer = imageFile.FileData.get_BinaryData() as byte[]; ms.Write(buffer, 0, buffer.Length); } else { return(false); } Color c1 = new Color(); Color c2 = new Color(); Color c3 = new Color(); Color c4 = new Color(); Color c5 = new Color(); Color c6 = new Color(); Color c7 = new Color(); Color c8 = new Color(); Color c9 = new Color(); int rr, r1, r2, r3, r4, r5, r6, r7, r8, r9, fxr, i, j; // int g1, g2, g3, g4, fxg, fyg, b1, b2, b3, b4, fxb, fyb; Bitmap imgTem = (Bitmap)Bitmap.FromStream(ms); for (i = 1; i < imgTem.Width - 2; i++) { for (j = 1; j < imgTem.Height - 2; j++) { c1 = imgTem.GetPixel(i, j - 1); c2 = imgTem.GetPixel(i - 1, j); c3 = imgTem.GetPixel(i, j); c4 = imgTem.GetPixel(i + 1, j); c5 = imgTem.GetPixel(i, j + 1); c6 = imgTem.GetPixel(i - 1, j - 1); c7 = imgTem.GetPixel(i - 1, j + 1); c8 = imgTem.GetPixel(i + 1, j - 1); c9 = imgTem.GetPixel(i + 1, j + 1); r1 = c1.R; r2 = c2.R; r3 = c3.R; r4 = c4.R; r5 = c5.R; r6 = c6.R; r7 = c7.R; r8 = c8.R; r9 = c9.R; fxr = 8 * r3 - r1 - r2 - r4 - r5 - r6 - r7 - r8 - r9; // fyr = r6 + 2 * r1 + r8 - r7 - 2 * r5 - r9; rr = Math.Abs(fxr); if (rr < 0) { rr = 0; } if (rr > 255) { rr = 255; } Color cc = Color.FromArgb(rr, rr, rr); imgTem.SetPixel(i, j, cc); } } bool isc = false; Color pixel; //污点熟练 int maxc = 5; #region 扫描左空白 for (int x = 1; x < imgTem.Width - 2; x++) { int c = 0; for (int y = 1; y < imgTem.Height - 2; y++) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { isc = true; } } if (isc == false) { xLeft = x; break; } } #endregion #region 扫描右空白 for (int x = imgTem.Width - 2; x > 2; x--) { int c = 0; for (int y = imgTem.Height - 2; y > 2; y--) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { isc = true; } } if (isc == false) { xRight = x; break; } } #endregion #region 扫描底部空白 for (int y = imgTem.Height - 2; y > 2; y--) { int c = 0; for (int x = imgTem.Width - 2; x > 2; x--) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { //imgTem = Clip(imgTem, 0, 0, x, imgTem.Height); //pictureimgTem.Image = imgTem; //pictureimgTem.Image = imgTem; isc = true; } } if (isc == false) { yBotom = y; break; } } #endregion #region 扫描上部空白 for (int y = 1; y < imgTem.Height - 2; y++) { int c = 0; for (int x = 1; x < imgTem.Width - 2; x++) { pixel = imgTem.GetPixel(x, y); int r, g, b; r = pixel.R; g = pixel.G; b = pixel.B; if (r == 255 && g == 255 && b == 255) { c++; if (c >= maxc) { isc = false; break; } } else { isc = true; } } if (isc == false) { yTotp = y; break; } } #endregion //剪切图片 imgTem = (Bitmap)Bitmap.FromStream(ms); imgTem = Clip(imgTem, xLeft - 5, yTotp - 5, xRight - xLeft + 5, yBotom - yTotp + 5); string strCurpath = System.IO.Directory.GetCurrentDirectory() + "\\scantdtk.bmp"; imgTem.Save(strCurpath, ImageFormat.Bmp); byteImage = System.IO.File.ReadAllBytes(strCurpath); return(true); }
private string GetDeviceInfo(string deviceId) { if (string.IsNullOrEmpty(deviceId)) { return DeviceNotSpecified; } DeviceManager deviceManager = new DeviceManagerClass(); var deviceInfo = deviceManager.DeviceInfos.Cast<DeviceInfo>().FirstOrDefault(p => p.DeviceID == deviceId); if (deviceInfo == null) { return DeviceNotFound; } // ReSharper disable once UseIndexedProperty var company = (string)deviceInfo.Properties["Manufacturer"].get_Value(); // ReSharper disable once UseIndexedProperty var name = (string)deviceInfo.Properties["Name"].get_Value(); return string.Format("{0} ({1})", name, company); }
private void LoadConfig()//загрузка конфигурации из файла { var settings = File.ReadAllLines(Config); var mode = loadMode.undef; foreach (var setting in settings) { if (setting.StartsWith("[device]")) { mode = loadMode.device; continue; } if (setting.StartsWith("[item]")) { mode = loadMode.item; continue; } if (setting.StartsWith("DeviceID")) { var deviceid = setting.Split(';')[1]; var devMngr = new DeviceManagerClass(); foreach (IDeviceInfo deviceInfo in devMngr.DeviceInfos) { if (deviceInfo.DeviceID == deviceid) { _scanDevice = deviceInfo.Connect(); break; } } if (_scanDevice == null) { MessageBox.Show("Сканнер из конигурации не найден"); return; } _scannerItem = _scanDevice.Items[1]; continue; } if (setting.StartsWith("ItemID")) { var itemid = setting.Split(';')[1]; continue; } var sett = setting.Split(';'); switch (mode) { case loadMode.device: SetProp(_scanDevice.Properties, sett[1], sett[2]); break; case loadMode.item: SetProp(_scannerItem.Properties, sett[1], sett[2]); break; } } SaveProp(_scanDevice.Properties, ref _defaultDeviceProp); }
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; }