在C#中使用没有对话框的扫描仪

我正在构建一个.Net 4.0应用程序,用于远程控制扫描仪设备。 我已经尝试了TWAIN和WIA库,但我遇到了同样的问题。 扫描图像时无需 扫描仪选择扫描设置对话框

我在.Net上找到了一篇关于WIA脚本的有用文章,并将其修改为:

private Image Scan(string deviceName) { WiaClass wiaManager = null; // WIA manager COM object CollectionClass wiaDevs = null; // WIA devices collection COM object ItemClass wiaRoot = null; // WIA root device COM object CollectionClass wiaPics = null; // WIA collection COM object ItemClass wiaItem = null; // WIA image COM object try { // create COM instance of WIA manager wiaManager = new WiaClass(); // call Wia.Devices to get all devices wiaDevs = wiaManager.Devices as CollectionClass; if ((wiaDevs == null) || (wiaDevs.Count == 0)) { throw new Exception("No WIA devices found!"); } object device = null; foreach (IWiaDeviceInfo currentDevice in wiaManager.Devices) { if (currentDevice.Name == deviceName) { device = currentDevice; break; } } if (device == null) { throw new Exception ( "Device with name \"" + deviceName + "\" could not be found." ); } // select device wiaRoot = (ItemClass)wiaManager.Create(ref device); // something went wrong if (wiaRoot == null) { throw new Exception ( "Could not initialize device \"" + deviceName + "\"." ); } wiaPics = wiaRoot.GetItemsFromUI ( WiaFlag.SingleImage, WiaIntent.ImageTypeColor ) as CollectionClass; if (wiaPics == null || wiaPics.Count == 0) { throw new Exception("Could not scan image."); } Image image = null; // enumerate all the pictures the user selected foreach (object wiaObj in wiaPics) { if (image == null) { wiaItem = (ItemClass)Marshal.CreateWrapperOfType ( wiaObj, typeof(ItemClass) ); // create temporary file for image string tempFile = Path.GetTempFileName(); // transfer picture to our temporary file wiaItem.Transfer(tempFile, false); // create Image instance from file image = Image.FromFile(tempFile); } // release enumerated COM object Marshal.ReleaseComObject(wiaObj); } if (image == null) { throw new Exception("Error reading scanned image."); } return image; } finally { // release WIA image COM object if (wiaItem != null) Marshal.ReleaseComObject(wiaItem); // release WIA collection COM object if (wiaPics != null) Marshal.ReleaseComObject(wiaPics); // release WIA root device COM object if (wiaRoot != null) Marshal.ReleaseComObject(wiaRoot); // release WIA devices collection COM object if (wiaDevs != null) Marshal.ReleaseComObject(wiaDevs); // release WIA manager COM object if (wiaManager != null) Marshal.ReleaseComObject(wiaManager); } } 

有了这个,我实际上设法从配置中选择设备(Scan方法的输入参数)并在扫描后检索生成的图像。

但扫描选项对话框的问题(使用DEVICENAME扫描)。 由于这是一个远程控制应用程序,用户不会看到对话框,因此我需要使用默认设置跳过它,或者在必要时使用配置中的设置。

扫描选项对话框: 扫描选项对话框

最后,我没有使用问题中编写的代码来扫描对话框。 我找到了一个使用Windows Image Acquisition 2.0库扫描的有用示例,并对其进行了修改以满足我的需求。 扫描没有任何对话框。

我写了一篇关于在.Net中使用没有对话框的扫描程序的文章和一个用C#编写的示例应用程序。

首先,非常感谢Miljenko Barbir的上述解决方案,它的效果非常好。

我想补充说,如果你想要零对话框,你可以使用(来自Milijenko的演示代码)

 WIA.ImageFile image = item.Transfer(wiaFormatBMP); 

代替

 WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false); 

这基本上也删除了进度条,因此您可以在没有任何对话框的情况下进行扫描。

 // show scanner view guif.ShowUI = 0; guif.ModalUI = 0; 

您可以在此代码中看到我已实现的内容。