C# 使用TWAIN协议进行扫描仪操作

折腾两台扫描仪程序的修改,今天有空把程序中涉及到扫描仪操作的代码贴出来,供大家一起交流学习。

 1 public ArrayList TransferPictures()
 2         {
 3             ArrayList pics = new ArrayList();
 4             if (srcds.Id == IntPtr.Zero)
 5                 return pics;
 6 
 7             TwRC rc;
 8             IntPtr hbitmap = IntPtr.Zero;
 9             TwPendingXfers pxfr = new TwPendingXfers();
10 
11             do
12             {
13                 pxfr.Count = 0;
14                 hbitmap = IntPtr.Zero;
15 
16                 TwImageInfo iinf = new TwImageInfo();//扫描仪设置的扫描图片信息
17 
18                 rc = DSiinf(appid, srcds, TwDG.Image, TwDAT.ImageInfo, TwMSG.Get, iinf);//获取扫描图片信息
19                 if (rc != TwRC.Success)
20                 {
21                     CloseSrc();
22                     return pics;
23                 }
24 
25 
26                 rc = DSixfer(appid, srcds, TwDG.Image, TwDAT.ImageNativeXfer, TwMSG.Get, ref hbitmap);//扫描程序,如果扫描完成,则返回XferDone
27                 if (rc != TwRC.XferDone)
28                 {
29                     CloseSrc();
30                     return pics;
31                 }
32 
33 
34                 rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.EndXfer, pxfr);//结束扫描
35                 if (rc != TwRC.Success)
36                 {
37                     CloseSrc();
38                     return pics;
39                 }
40 
41                 pics.Add(hbitmap);
42             }
43             while (pxfr.Count != 0);
44 
45             rc = DSpxfer(appid, srcds, TwDG.Control, TwDAT.PendingXfers, TwMSG.Reset, pxfr);
46             return pics;
47         }
posted @ 2012-07-06 17:01  Peter.Luo  阅读(6193)  评论(3编辑  收藏  举报