I am trying to scan multiple pages using an auto-feeder scanner. My code is very simple at the moment:
WIA.CommonDialog dialog = new WIA.CommonDialog();
WIA.Device device = dialog.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType);
WIA.Items items = dialog.ShowSelectItems(device);
foreach (WIA.Item item in items)
{
while (true)
{
try
{
WIA.ImageFile image = (WIA.ImageFile)dialog.ShowTransfer(item);
if (image != null && image.FileData != null)
{
dynamic binaryData = image.FileData.get_BinaryData();
if (binaryData is byte[])
using (MemoryStream stream = new MemoryStream(binaryData))
using (Bitmap bitmap = (Bitmap)Bitmap.FromStream(stream))
{
bitmap.Save(@"C:\Temp\scan.jpg", ImageFormat.Jpeg);
}
}
}
catch (COMException)
{
break;
}
}
}
I tried to request a property WIA_DPS_DOCUMENT_HANDLING_STATUSto see if there are any pages in the feeder, but it always returns 1, so instead I will catch a COM exception, if I get it WIA_ERROR_PAPER_EMPTY, I know that the document feeder is empty.
The problem is that this code only looks at the first page, when the method dialog.ShowTransferis called again, I get an exception and E_FAILHResult, and I can not scan more pages. Oddly enough, when I look at this code in the debugger, everything works fine, and all pages are scanned.
, Marshal.ReleaseComObject(image) image = null, . . -, , ?
EDIT: . E_FAIL, , ( ). , , 10 , , , .
2: , -, WIA . -, .