WIA

一台扫描仪,实际上就是一个Device对象,因此,我们可以通过DeviceManager来“获取”这台设备的“引用”,然后通过得到的Device对象,执行相应的扫描工作。从而跳过了使用ShowAcquireImage方法带来的一系列“多余的鼠标操作问题”。

获取Device对象

按照上面思路,首先需要建立一个DeviceManager对象:

DeviceManager manager = new DeviceManagerClass();然后获取Device对象,在这里,我假设我的电脑上只有一台扫描仪,因此不做诸如“判断使用哪台扫描仪进行扫描”之类的操作。

Device device = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
    device = info.Connect();
    break;
}

扫描图像

WIA把Device设备的图像数据看做一个个Item对象,可以通过方法GetItem(ItemID)来实现。不过,对于扫描仪做种东西,和数码相机不同,一般只有一个Item对象,因此可以简单的使用数组的方法(注意:index是从1开始的,而不是从0):

Item item = device.Items[1];

最后,调用CommonDialog的ShowTransfer方法,用一个进度条,来显示扫描过程:

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 (MemoryStream ms = new MemoryStream())
    {
        ms.Write(buffer, 0, buffer.Length);
        pictureBox1.Image = Image.FromStream(ms);
    }
}
posted @ 2015-07-21 16:06  HongMaJu  阅读(1149)  评论(1编辑  收藏  举报