private void btnScan_Click(object sender, EventArgs e) { ImageFile imageFile = null; CommonDialogClass cdc = new WIA.CommonDialogClass(); try { imageFile = cdc.ShowAcquireImage(WIA.WiaDeviceType.ScannerDeviceType, WIA.WiaImageIntent.TextIntent, WIA.WiaImageBias.MaximizeQuality, "{00000000-0000-0000-0000-000000000000}", false, false, false); } catch (System.Runtime.InteropServices.COMException) { imageFile = null; } if (imageFile != null) { // SaveFileDialog s = new SaveFileDialog(); //s.ShowDialog(); // imageFile.SaveFile(s.FileName); //using (FileStream stream = new FileStream(s.FileName, FileMode.Open, // FileAccess.Read, FileShare.Read)) //{ // pictureBox1.Image = Image.FromFile(s.FileName); // } } 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); } } }
/// <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); } }