Scan with WIA Auto Feed Scanner not performed for second page

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 . -, .

+4
2

, : http://www.codeproject.com/Tips/792316/WIA-Scanner-in-Csharp-Windows-Forms

hp scanjet 5590, , . dialog.ShowTransfer ArgumentException " ". , "" "", . . , :

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)
            {
                WIA.ImageFile image = null;
                try
                {
                    dialog = new WIA.CommonDialog();
                    image = (WIA.ImageFile)dialog.ShowTransfer(item,"{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}", false);
                    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(String.Format(@"C:\Temp\scan{0}.jpg", Path.GetRandomFileName()),
                                    ImageFormat.Jpeg);
                            }
                    }
                }
                catch (COMException)
                {
                    break;
                }
                finally
                {
                    if (image != null)
                        Marshal.FinalReleaseComObject(image);
                }
            }
        }

CodeProject , . , , :

    public static List<Image> Scan(string scannerId)
    {
        List<Image> images = new List<Image>();

        // select the correct scanner using the provided scannerId parameter
        WIA.DeviceManager manager = new WIA.DeviceManager();
        WIA.Device device = null;
        foreach (WIA.DeviceInfo info in manager.DeviceInfos)
        {
            if (info.DeviceID == scannerId)
            {
                // connect to scanner
                device = info.Connect();
                break;
            }
        }
        // device was not found
        if (device == null)
        {
            // enumerate available devices
            string availableDevices = "";
            foreach (WIA.DeviceInfo info in manager.DeviceInfos)
            {
                availableDevices += info.DeviceID + "\n";
            }

            // show error with available devices
            throw new Exception("The device with provided ID could not be found. Available Devices:\n" + availableDevices);
        }

        WIA.Item item = null;
        WIA.CommonDialog dialog = new WIA.CommonDialog();
        WIA.Items items = dialog.ShowSelectItems(device);
        if (items == null)
            return images;

        item = items[1];

        bool hasMorePages = true;
        while (hasMorePages)
        {
            try
            {
                // scan image
                WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
                WIA.ImageFile image = (WIA.ImageFile)wiaCommonDialog.ShowTransfer(item, wiaFormatBMP, false);

                // save to temp file
                string fileName = Path.GetTempFileName();
                File.Delete(fileName);
                image.SaveFile(fileName);
                try
                {
                    Marshal.FinalReleaseComObject(image);
                }
                finally
                {
                    image = null;
                }
                // add file to output list
                images.Add(Image.FromFile(fileName));
            }
            finally
            {
                //determine if there are any more pages waiting
                WIA.Property documentHandlingSelect = null;
                WIA.Property documentHandlingStatus = null;
                foreach (WIA.Property prop in device.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;
                }
                // assume there are no more pages
                hasMorePages = false;
                // may not exist on flatbed scanner but required for feeder
                if (documentHandlingSelect != null)
                {
                    // 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);
                    }
                }
            }
        }
        return images;
    }

btn_scan_Click :

    private void btn_scan_Click(object sender, EventArgs e)
    {

        try
        {
            //get list of devices available
            List<string> devices = WIAScanner.GetDevices();
            List<Image> images = null;

            //check if device is not available
            if (devices.Count == 0)
            {
                MessageBox.Show("You do not have any WIA devices.");
                this.Close();
            }
            else
            {
                if (devices.Count == 1)
                {
                    images = WIAScanner.Scan(devices[0]);
                }
                else
                {
                    images = WIAScanner.Scan();
                }
            }
            //get images from scanner
            foreach (Image image in images)
            {
                var path = String.Format(@"C:\Temp\scan{0}_{1}.jpg", DateTime.Now.ToString("yyyy-MM-dd HHmmss"), Path.GetRandomFileName());
                image.Save(path, ImageFormat.Jpeg);
            }
        }
        catch (Exception exc)
        {
            MessageBox.Show(exc.Message);
        }
    }
+1

     try
     {
        // Create a DeviceManager instance
        var deviceManager = new DeviceManager();

        List<Image> ret = new List<Image>();

        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)
                 {
                    var imageBytes = (byte[]) image.FileData.get_BinaryData();
                    var ms = new MemoryStream(imageBytes);
                    Image img = null;
                    img = Image.FromStream(ms);

                    ret.Add(img);
                 }
              }
              catch
              {
                 break;
              }
           }
        }
        return ret;
     }
     catch (Exception)
     {
        return null;
     }
-1

Source: https://habr.com/ru/post/1608420/


All Articles