当前位置:编程学习 > C#/ASP.NET >>

调用WIA进行扫描仪操作,WIN7系统正常,XP系统报错

贴代码:

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(),
                true) as 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}的组件时失败,原因是出现以下错误:80040154

XP系统中安装了WIA驱动、.Net 3.5、.Net 4.0都不行。求助 --------------------编程问答-------------------- 已经解决。
从网上找一个wiaaut.dll文件,使用Regsvr32进行注册即可。 --------------------编程问答-------------------- 给分啦 --------------------编程问答-------------------- 还是这段代码
生成代码后,拿到客户端PC运行,扫描仪认了,但是提示“值不在预期的范围内”,检测出错代码行为0。
不死心,给这台客户端PC安装VS2008准备调试,结果VS安装完,错误木有了,一切正常了。断点调试也查不出来 --------------------编程问答-------------------- 我也正在做这一块,wia能 支持扫描 多张图片吗?如果扫描多张 是如何保存的呢,求楼主解答
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,