使用 WIA 通过文档进纸器扫描多页
Scanning multiple pages via document feeder with WIA
在遵循一些示例并检查解决此错误的方法以及其他问题post后无法正常工作,但目前没有任何效果。
它扫描第一页,当代码循环到它应该获取第二张图像的地方时,它总是抛出异常,说设备忙,无论如何在每页之后暂停或停止文档进纸器中的扫描防止此异常?
string deviceid;
List<ImageFile> images = new List<ImageFile>();
CommonDialogClass dlg = new CommonDialogClass();
Device d = dlg.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
if (d != null)
{
deviceid = d.DeviceID;
}
else
{
//no scanner chosen
return;
}
bool hasMorePages = true;
int x = 0;
int numPages = 0;
while (hasMorePages)
{
DeviceManager manager = new DeviceManagerClass();
Device WiaDev = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == deviceid)
{
WIA.Properties infoprop = null;
infoprop = info.Properties;
WiaDev = info.Connect();
break;
}
}
SelectDeviceDocumentHandling(WiaDev, DeviceDocumentHandling.Feeder);
SetDeviceProperty(WiaDev, DEVICE_PROPERTY_PAGES_ID, 1);
ImageFile img = null;
Item Item = WiaDev.Items[1];
try
{
WIA.CommonDialog WiaCommonDialog = new WIA.CommonDialog();
img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, FormatID.wiaFormatPNG, false);
images.Add(img);
numPages++;
img = null;
}
catch (Exception ex) //ex.Message "0x80210003" means no documents found in feeder
{
if (ex.Message.Contains("0x80210015")) MessageBox.Show("Could not connect to scanner", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (ex.Message.Contains("0x80210006")) MessageBox.Show("Device is busy", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //where I get exception on second page each time
else if (ex.Message.Contains("0x80210003")) MessageBox.Show("No documents found in document feeder", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Item = null;
//determine if there are any more pages waiting
Property documentHandlingSelect = null;
Property documentHandlingStatus = null;
foreach (Property prop in WiaDev.Properties)
{
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
hasMorePages = false; //assume there are no more pages
if (documentHandlingSelect != null)
//may not exist on flatbed scanner but required for feeder
{
//check for document feeder
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
{
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
}
}
x++;
}
}
我已经尝试了以下 post 的解决方案,但没有成功,并且在网络上进行了大量搜索:
Scanning with WIA automatic feeder scanner fails for second page
想知道这是否也是我的特定扫描仪的问题,我在家工作,但下周回到办公室后将能够在工作扫描仪上进行测试。
发现问题,是程序出于某种原因使用 WSD 连接到扫描仪时出现的问题。当我通过 USB 电缆连接时,我没有遇到这个问题,因为它使用了制造商的驱动程序。
在遵循一些示例并检查解决此错误的方法以及其他问题post后无法正常工作,但目前没有任何效果。
它扫描第一页,当代码循环到它应该获取第二张图像的地方时,它总是抛出异常,说设备忙,无论如何在每页之后暂停或停止文档进纸器中的扫描防止此异常?
string deviceid;
List<ImageFile> images = new List<ImageFile>();
CommonDialogClass dlg = new CommonDialogClass();
Device d = dlg.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
if (d != null)
{
deviceid = d.DeviceID;
}
else
{
//no scanner chosen
return;
}
bool hasMorePages = true;
int x = 0;
int numPages = 0;
while (hasMorePages)
{
DeviceManager manager = new DeviceManagerClass();
Device WiaDev = null;
foreach (DeviceInfo info in manager.DeviceInfos)
{
if (info.DeviceID == deviceid)
{
WIA.Properties infoprop = null;
infoprop = info.Properties;
WiaDev = info.Connect();
break;
}
}
SelectDeviceDocumentHandling(WiaDev, DeviceDocumentHandling.Feeder);
SetDeviceProperty(WiaDev, DEVICE_PROPERTY_PAGES_ID, 1);
ImageFile img = null;
Item Item = WiaDev.Items[1];
try
{
WIA.CommonDialog WiaCommonDialog = new WIA.CommonDialog();
img = (ImageFile)WiaCommonDialog.ShowTransfer(Item, FormatID.wiaFormatPNG, false);
images.Add(img);
numPages++;
img = null;
}
catch (Exception ex) //ex.Message "0x80210003" means no documents found in feeder
{
if (ex.Message.Contains("0x80210015")) MessageBox.Show("Could not connect to scanner", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else if (ex.Message.Contains("0x80210006")) MessageBox.Show("Device is busy", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); //where I get exception on second page each time
else if (ex.Message.Contains("0x80210003")) MessageBox.Show("No documents found in document feeder", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Item = null;
//determine if there are any more pages waiting
Property documentHandlingSelect = null;
Property documentHandlingStatus = null;
foreach (Property prop in WiaDev.Properties)
{
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_SELECT)
documentHandlingSelect = prop;
if (prop.PropertyID == WIA_PROPERTIES.WIA_DPS_DOCUMENT_HANDLING_STATUS)
documentHandlingStatus = prop;
}
hasMorePages = false; //assume there are no more pages
if (documentHandlingSelect != null)
//may not exist on flatbed scanner but required for feeder
{
//check for document feeder
if ((Convert.ToUInt32(documentHandlingSelect.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_SELECT.FEEDER) != 0)
{
hasMorePages = ((Convert.ToUInt32(documentHandlingStatus.get_Value()) & WIA_DPS_DOCUMENT_HANDLING_STATUS.FEED_READY) != 0);
}
}
x++;
}
}
我已经尝试了以下 post 的解决方案,但没有成功,并且在网络上进行了大量搜索: Scanning with WIA automatic feeder scanner fails for second page
想知道这是否也是我的特定扫描仪的问题,我在家工作,但下周回到办公室后将能够在工作扫描仪上进行测试。
发现问题,是程序出于某种原因使用 WSD 连接到扫描仪时出现的问题。当我通过 USB 电缆连接时,我没有遇到这个问题,因为它使用了制造商的驱动程序。