WIA设备批量扫描
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using WIA;
- using System.IO;
- using System.Drawing.Imaging;
- using iTextSharp;
- using iTextSharp.text;
- using iTextSharp.text.pdf;
- using System.Drawing.Drawing2D;
- namespace printTest
- {
- public partial class Form1 : Form
- {
- List<System.Drawing.Image> images = new List<System.Drawing.Image>();
- 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;
- }
- const string wiaFormatBMP = "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}";
- class WIA_DPS_DOCUMENT_HANDLING_SELECT
- {
- public const uint FEEDER = 0x00000001;
- public const uint FLATBED = 0x00000002;
- }
- class WIA_DPS_DOCUMENT_HANDLING_STATUS
- {
- public const uint FEED_READY = 0x00000001;
- }
- class WIA_PROPERTIES
- {
- public const uint WIA_RESERVED_FOR_NEW_PROPS = ;
- public const uint WIA_DIP_FIRST = ;
- public const uint WIA_DPA_FIRST = WIA_DIP_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
- public const uint WIA_DPC_FIRST = WIA_DPA_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
- //
- // Scanner only device properties (DPS)
- //
- public const uint WIA_DPS_FIRST = WIA_DPC_FIRST + WIA_RESERVED_FOR_NEW_PROPS;
- public const uint WIA_DPS_DOCUMENT_HANDLING_STATUS = WIA_DPS_FIRST + ;
- public const uint WIA_DPS_DOCUMENT_HANDLING_SELECT = WIA_DPS_FIRST + ;
- }
- #region 画矩形使用到的变量
- Point p1; //定义鼠标按下时的坐标点
- Point p2; //定义移动鼠标时的坐标点
- Point p3; //定义松开鼠标时的坐标点
- #endregion
- #region 缩放、裁剪图像用到的变量
- /// <summary>
- ///
- /// </summary>
- int x1; //鼠标按下时横坐标
- int y1; //鼠标按下时纵坐标
- int width; //所打开的图像的宽
- int heigth; //所打开的图像的高
- bool HeadImageBool = false; // 此布尔变量用来判断pictureBox1控件是否有图片
- #endregion
- Bitmap Bi; //定义位图对像
- public Form1()
- {
- InitializeComponent();
- }
- #region 扫描1
- private void btnScan_Click(object sender, EventArgs e)
- {
- listBox1.Items.Clear();
- lbDevices.Items.Clear();
- List<string> devices = GetDevices();
- foreach (string device in devices)
- {
- lbDevices.Items.Add(device);
- }
- //check if device is not available
- if (lbDevices.Items.Count == )
- {
- MessageBox.Show("没有任何可用的打印、扫描设备!");
- this.Close();
- }
- else
- {
- lbDevices.SelectedIndex = ;
- }
- bool hasMorePages = true;
- string deviceID = (string)lbDevices.SelectedItem;
- 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);
- }
- int i = device.Items.Count;
- WIA.Item item = device.Items[] 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 = Path.GetTempFileName();
- File.Delete(fileName);
- image.SaveFile(fileName);//保存temp文件
- image = null;
- int leng = @"C:\Users\ll\AppData\Local\Temp".Length;
- // add file to output list
- listBox1.Items.Add(fileName.Substring(leng, fileName.Length - leng));
- //listBox1.Items.Add(fileName);
- images.Add(System.Drawing.Image.FromFile(fileName));//C:\Users\ll\AppData\Local\Temp
- }
- catch (Exception exc)
- {
- //MessageBox.Show(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) != )
- {
- hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != );
- }
- }
- }
- }
- if (images.Count >= )
- {
- pictureBox1.Image = images[];
- double imageX = pictureBox1.Image.Width;
- double imageY = pictureBox1.Image.Height;
- double picX = pictureBox1.Width;
- double pixY = pictureBox1.Height;
- //pictureBox2.Image = images[0];
- //pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- }
- }
- #endregion
- #region 保存
- private void button1_Click(object sender, EventArgs e)
- {
- if (pictureBox2.Image != null)
- {
- string where = @"E:\" + DateTime.Now.ToString("yyyy-MM-dd HHmmss");
- //save scanned image into specific folder
- pictureBox2.Image.Save(where + ".jpeg", ImageFormat.Jpeg);
- // //iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(216f, 716f);
- // iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(216f, 716f);
- // pageSize.BackgroundColor = new iTextSharp.text.BaseColor(0xFF, 0xFF, 0xDE);
- // iTextSharp.text.Image img1 = iTextSharp.text.Image.GetInstance(pictureBox1.Image, iTextSharp.text.BaseColor.WHITE);
- // //设置边界
- // //Document document = new Document(pageSize, 36f, 72f, 108f, 180f);
- // Document document = new Document(pageSize, 36f, 72f, 108f, 180f);
- // PdfWriter.GetInstance(document, new FileStream(where + ".pdf", FileMode.Create));
- // // 添加文档信息
- // document.AddTitle("PDFInfo");
- // document.AddSubject("Demo of PDFInfo");
- // document.AddKeywords("Info, PDF, Demo");
- // document.AddCreator("SetPdfInfoDemo");
- // document.AddAuthor("zeda");
- // document.Open();
- // // 添加文档内容
- // document.Add(img1);
- // document.Close();
- }
- }
- #endregion
- #region 顺时针旋转
- private void button4_Click(object sender, EventArgs e)
- {
- if (pictureBox1.Image != null)
- {
- try
- {
- Bitmap a = new Bitmap(pictureBox1.Image);//得到图片框中的图片
- pictureBox1.Image = Rotate(a, );
- //pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBox1.Location = panel1.Location;
- pictureBox1.Refresh();//最后刷新图片框
- }
- catch { }
- }
- }
- #endregion
- #region 扫描4
- private void button5_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}",
- true,
- true,
- false);
- string fileName = Path.GetTempFileName();
- File.Delete(fileName);
- if (imageFile != null)
- {
- imageFile.SaveFile(fileName);//保存temp文件
- imageFile = null;
- int leng = @"C:\Users\ll\AppData\Local\Temp".Length;
- // add file to output list
- listBox1.Items.Add(fileName.Substring(leng, fileName.Length - leng));
- //listBox1.Items.Add(fileName);
- images.Add(System.Drawing.Image.FromFile(fileName));//C:\Users\ll\AppData\Local\Temp
- foreach (System.Drawing.Image im in images)
- {
- string where = @"E:\" + DateTime.Now.ToString("yyyy-MM-dd HHmmss");
- //save scanned image into specific folder
- im.Save(where + ".jpeg", ImageFormat.Jpeg);
- }
- }
- }
- catch (System.Runtime.InteropServices.COMException)
- {
- imageFile = null;
- }
- }
- #endregion
- int i = ;
- #region 增加
- private void button2_Click(object sender, EventArgs e)
- {
- i++;
- label1.Text = i.ToString();
- if (i < images.Count && i >= )
- {
- pictureBox1.Image = images[i];
- //pictureBox2.Image = images[i];
- //pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- }
- }
- #endregion
- #region 减小
- private void button3_Click(object sender, EventArgs e)
- {
- i--;
- label1.Text = i.ToString();
- if (i < images.Count && i >= )
- {
- pictureBox1.Image = images[i];
- //pictureBox2.Image = images[i];
- //pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- }
- }
- #endregion
- #region 旋转
- private void button6_Click(object sender, EventArgs e)
- {
- if (pictureBox1.Image != null)
- {
- try
- {
- Bitmap a = new Bitmap(pictureBox1.Image);//得到图片框中的图片
- pictureBox1.Image = Rotate(a, -);
- //pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
- pictureBox1.Location = panel1.Location;
- pictureBox1.Refresh();//最后刷新图片框
- }
- catch { }
- }
- }
- #endregion
- #region 图片旋转函数
- /// <summary>
- /// 以逆时针为方向对图像进行旋转
- /// </summary>
- /// <param name="b">位图流</param>
- /// <param name="angle">旋转角度[0,360](前台给的)</param>
- /// <returns></returns>
- public Bitmap Rotate(Bitmap b, int angle)
- {
- angle = angle % ;
- //弧度转换
- double radian = angle * Math.PI / 180.0;
- double cos = Math.Cos(radian);
- double sin = Math.Sin(radian);
- //原图的宽和高
- int w = b.Width;
- int h = b.Height;
- int W = (int)(Math.Max(Math.Abs(w * cos - h * sin), Math.Abs(w * cos + h * sin)));
- int H = (int)(Math.Max(Math.Abs(w * sin - h * cos), Math.Abs(w * sin + h * cos)));
- //目标位图
- Bitmap dsImage = new Bitmap(W, H);
- Graphics g = Graphics.FromImage(dsImage);
- g.InterpolationMode = InterpolationMode.Bilinear;
- g.SmoothingMode = SmoothingMode.HighQuality;
- //计算偏移量
- Point Offset = new Point((W - w) / , (H - h) / );
- //构造图像显示区域:让图像的中心与窗口的中心点一致
- System.Drawing.Rectangle rect = new System.Drawing.Rectangle(Offset.X, Offset.Y, w, h);
- Point center = new Point(rect.X + rect.Width / , rect.Y + rect.Height / );
- g.TranslateTransform(center.X, center.Y);
- g.RotateTransform( - angle);
- //恢复图像在水平和垂直方向的平移
- g.TranslateTransform(-center.X, -center.Y);
- g.DrawImage(b, rect);
- //重至绘图的所有变换
- g.ResetTransform();
- g.Save();
- g.Dispose();
- return dsImage;
- }
- #endregion 图片旋转函数更多 0
- private void Form1_Load(object sender, EventArgs e)
- {
- #region 滚动条
- panel2.AutoScroll = true;
- pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
- panel3.AutoScroll = true;
- pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
- #endregion
- #region 自动缩放
- //pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- //pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
- //pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
- //pictureBox2.Dock = System.Windows.Forms.DockStyle.Fill;
- #endregion
- }
- private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
- {
- this.Cursor = Cursors.Cross;
- this.p1 = new Point(e.X, e.Y);
- x1 = e.X;
- y1 = e.Y;
- if (this.pictureBox1.Image != null)
- {
- HeadImageBool = true;
- }
- else
- {
- HeadImageBool = false;
- }
- }
- #region 移动鼠标发生的事件
- /// <summary>
- /// 鼠标移动事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
- {
- if (this.Cursor == Cursors.Cross)
- {
- this.p2 = new Point(e.X, e.Y);
- if ((p2.X - p1.X) > && (p2.Y - p1.Y) > ) //当鼠标从左上角向开始移动时P3坐标
- {
- this.p3 = new Point(p1.X, p1.Y);
- }
- if ((p2.X - p1.X) < && (p2.Y - p1.Y) > ) //当鼠标从右上角向左下方向开始移动时P3坐标
- {
- this.p3 = new Point(p2.X, p1.Y);
- }
- if ((p2.X - p1.X) > && (p2.Y - p1.Y) < ) //当鼠标从左下角向上开始移动时P3坐标
- {
- this.p3 = new Point(p1.X, p2.Y);
- }
- if ((p2.X - p1.X) < && (p2.Y - p1.Y) < ) //当鼠标从右下角向左方向上开始移动时P3坐标
- {
- this.p3 = new Point(p2.X, p2.Y);
- }
- this.pictureBox1.Invalidate(); //使控件的整个图面无效,并导致重绘控件
- }
- }
- #endregion
- #region 松开鼠标发生的事件,实例化ImageCut1类对像
- ImageCut1 IC1; //定义所画矩形的图像对像
- /// <summary>
- /// 鼠标放开事件
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
- {
- if (HeadImageBool)
- {
- width = this.pictureBox1.Image.Width;
- heigth = this.pictureBox1.Image.Height;
- if ((e.X - x1) > && (e.Y - y1) > ) //当鼠标从左上角向右下方向开始移动时发生
- {
- IC1 = new ImageCut1(x1, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
- }
- if ((e.X - x1) < && (e.Y - y1) > ) //当鼠标从右上角向左下方向开始移动时发生
- {
- IC1 = new ImageCut1(e.X, y1, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
- }
- if ((e.X - x1) > && (e.Y - y1) < ) //当鼠标从左下角向右上方向开始移动时发生
- {
- IC1 = new ImageCut1(x1, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
- }
- if ((e.X - x1) < && (e.Y - y1) < ) //当鼠标从右下角向左上方向开始移动时发生
- {
- IC1 = new ImageCut1(e.X, e.Y, Math.Abs(e.X - x1), Math.Abs(e.Y - y1)); //实例化ImageCut1类
- }
- if (IC1 != null)
- {
- this.pictureBox2.Width = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Width;
- this.pictureBox2.Height = (IC1.KiCut1((Bitmap)(this.pictureBox1.Image))).Height;
- this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
- //this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
- //boolShow1 = true;
- //else if (pictureBox3.Image == null)
- //{
- // this.pictureBox3.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
- // boolShow2 = true;
- //}
- //else
- //{
- // if (boolShow2 == true)
- // {
- // this.pictureBox2.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
- // boolShow1 = true;
- // boolShow2 = false;
- // }
- // else
- // {
- // this.pictureBox3.Image = IC1.KiCut1((Bitmap)(this.pictureBox1.Image));
- // boolShow1 = false;
- // boolShow2 = true;
- // }
- //}
- }
- this.Cursor = Cursors.Default;
- }
- else
- {
- this.Cursor = Cursors.Default;
- }
- }
- #endregion
- #region 重新绘制pictureBox1控件,即移动鼠标画矩形
- private void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- if (HeadImageBool)
- {
- Pen p = new Pen(Color.Black, );//画笔
- p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
- //Bitmap bitmap = new Bitmap(strHeadImagePath);
- Bitmap bitmap = Bi;
- System.Drawing.Rectangle rect = new System.Drawing.Rectangle(p3, new Size(System.Math.Abs(p2.X - p1.X), System.Math.Abs(p2.Y - p1.Y)));
- e.Graphics.DrawRectangle(p, rect);
- }
- else
- {
- }
- }
- #endregion
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- //string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");
- //string path = Path.GetFullPath("images/");
- //string[] files = Directory.GetFiles(path);
- //foreach (string file in files)
- //{
- // if (file == path + fileName + ".jpg")
- // {
- // return;
- // }
- //}
- //using (Stream stream = new FileStream(path + fileName + ".jpg", FileMode.Create))
- //{
- // using (FileStream fs = new FileStream(listBox1.SelectedItem.ToString(), FileMode.Open))
- // {
- // byte[] bytes = new byte[1024 * 10];
- // int len;
- // while ((len = fs.Read(bytes, 0, bytes.Length)) > 0)
- // {
- // stream.Write(bytes, 0, len);
- // }
- // }
- //}
- //System.Drawing.Image image = System.Drawing.Image.FromFile(path + fileName + ".jpg");
- //pictureBox1.Image = image;
- }
- }
- }
WIA设备批量扫描的更多相关文章
- 批量扫描互联网无线路由设备telnet,并获取WIFI密码
批量扫描互联网无线路由设备telnet,并获取WIFI密码 http://lcx.cc/?i=4513
- Burpsuite+sqlmap批量扫描sql漏洞
1.burpsuite设置导出log n'd'k 输入文件名保存 2.sqlmap批量扫描 python sqlmap.py -l 文件名 --batch -smart batch:自 ...
- BurpSuite导出log配合SQLMAP批量扫描注入点
sqlmap可以批量扫描包含有request的日志文件,而request日志文件可以通过burpsuite来获取, 因此通过sqlmap结合burpsuite工具,可以更加高效的对应用程序是否存在SQ ...
- JBOSS批量扫描
exploit-db提供出了EXP,如下: /* * JBoss JMXInvokerServlet Remote Command Execution * JMXInvoker.java v0.3 - ...
- sqlmap批量扫描burpsuite请求日志记录
sqlmap可以批量扫描包含有request的日志文件,而request日志文件可以通过burpsuite来获取, 因此通过sqlmap结合burpsuite工具,可以更加高效的对应用程序是否存在SQ ...
- BBScan — 一个信息泄漏批量扫描脚本
github:https://github.com/lijiejie/BBScan 有些朋友手上有几十万甚至上百万个域名,比如,乌云所有厂商的子域名. 如果把这30万个域名全部扔给wvs,APPsca ...
- wwwscan网站目录文件批量扫描工具
准备一个比赛样题里面给的一个扫描的工具: 不知道怎么用就上网百度了一下果然有关于这个软件的两篇介绍(感觉写的很好),第一篇介绍的应该和我的工具一样,也给了例子(现在Google不能访问了)和参数介绍, ...
- sqlmap批量扫描burpsuite拦截的日志记录
1.功能上,sqlmap具备对burpsuite拦截的request日志进行批量扫描的能力 python sqlmap.py -l hermes.log --batch -v 3 --batch:会自 ...
- 批量扫描IP端口程序 (适用于window&linux)
批量扫描IP端口,根据扫描IP导出IP命名的文件的结果.假设1.txt文件内容为127.0.0.1192.168.1.1然后我们获取文件内容IP进行扫描window .bat版本 :1.txt为文件名 ...
随机推荐
- php curl_init函数用法
原文地址:curl_init函数用法">php curl_init函数用法作者:loading 使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓 ...
- (转)JS获取当前对象大小以及屏幕分辨率等
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...
- PHP关闭Notice错误提示
原文出处 本文章介绍在php中关闭notice错误的一些做法,但是不得不说的是在关掉错误提示时,那么程序代码中大量的notice级别错误是否会造成PHP性能下降 PHP Notice: Undefin ...
- [压缩解压缩] SharpZip--压缩、解压缩帮助类
里面有三个类都是用于压缩和解压缩的.大家看下图片 看下面代码吧 /// <summary> /// 类说明:SharpZip /// 编 码 人:苏飞 /// 联系方式:361983679 ...
- SQL-Server索引漫谈
http://www.cnblogs.com/teroy/archive/2013/05/23/3070547.html
- [DB2]实现项目多数据库切换(上)--环境部署
基本软硬件信息:Windows 8.1 X64 / Microsoft Visual Studio 2012 / ThinkPad S3-S431 安装工具:IBM Data Studio 4.1. ...
- 理解Javascript__undefined和null
在 ECMAScript 的原始类型中,是有Undefined 和 Null 类型的. 这两种类型都分别对应了属于自己的唯一专用值,即undefined 和 null. alert(undefined ...
- [学习笔记]设计模式之Composite
为方便读者,本文已添加至索引: 设计模式 学习笔记索引 写在前面 在Composite(组合)模式中,用户可以使用多个简单的组件以形成较大的组件,而这些组件还可能进一步组合成更大的.它重要的特性是能够 ...
- 修正constructor的指向
function Aaa(){ } //Aaa.prototype.constructor = Aaa; //每一个函数都会有的,都是自动生成的 Aaa.prototype.name = '小明' ...
- 如何禁用easyui-linkbutton 中的Click事件
eg: <a id="btn" href="#" class="easyui-linkbutton" data-options=&qu ...