图形/图像
C#调用WIA对扫描仪进行开发编程
2017-02-05 14:45:33
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using WIA;//Com 引用 Microsoft Windows Image Acquisiton Library v2.0
 
namespace FileUpAndDown.baseCommon
{
    /// <summary>
    /// 扫描仪扫描
    /// </summary>
    public class ScanOper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ScanOper()
        {
            htFormtIDs = GetImageTypeIDHashTable();//获取数据
        }
        /// <summary>
        /// 存储扫描图像类型ID的哈希表
        /// </summary>
        private Hashtable htFormtIDs;
 
        #region [方法]
        /// <summary>
        /// 获取图片格式ID哈希表
        /// </summary>
        /// <returns></returns>
        private Hashtable GetImageTypeIDHashTable()
        {
            Hashtable ht = new Hashtable();
            ht.Add("wiaFormatBMP""{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}");
            ht.Add("wiaFormatPNG""{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}");
            ht.Add("wiaFormatGIF""{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}");
            ht.Add("wiaFormatJPEG""{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}");
            ht.Add("wiaFormatTIFF""{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}");
            return ht;
        }
 
        /// <summary>
        /// 开始扫描
        /// </summary>
        public Image ScanStart()
        {
            Image imgReturn = null;
            //检测扫描设备
            DeviceManager manager = new DeviceManagerClass();
            Device device = null;
            ImageFile imageFile = null;
            try
            {
                foreach (DeviceInfo info in manager.DeviceInfos)
                {
                    if (info.Type != WiaDeviceType.ScannerDeviceType) continue;
                    device = info.Connect();
                    break;
                }
                if (device == null)
                {
                    MessageBox.Show("未检测到扫描设备,请检查设备电源以及数据连接线!");
                    return null;
                }
                Item item = device.Items[1];
                CommonDialogClass cdc = new WIA.CommonDialogClass();
                //开始扫描
                imageFile = cdc.ShowTransfer(item,
                htFormtIDs["wiaFormatJPEG"].ToString(),
                trueas ImageFile;
            }
            catch (Exception ec)
            {
                MessageBox.Show("扫描出错!\n" + ec.Message.ToString());
                return null;
            }
            //将获取到得图像转为Image
            if (imageFile != null)
            {
                var buffer = imageFile.FileData.get_BinaryData() as byte[];
                using (MemoryStream ms = new MemoryStream())
                {
                    ms.Write(buffer, 0, buffer.Length);
                    imgReturn = Image.FromStream(ms);
                }
            }
            else
            {
                return null;
            }
            return imgReturn;
        }
        #endregion
    }
}

实例化ScanOper类时,XP系统报错:应用程序中发生了无法处理的异常。如果单击“继续”......检索COM类工厂中CLSID为{E1C5D730-7E97-4D8A-9E42-BBAE87C2059F}的组件时失败,原因是出现以下错误:80040154XP系统中安装了WIA驱动、.Net 3.5、.Net 4.0都不行。求助