cyanfish / naps2-wia

Low-level WIA 2.0/1.0 application library for .NET
MIT License
17 stars 5 forks source link

Error when trying to scan all pages #12

Open aditya-gune opened 1 month ago

aditya-gune commented 1 month ago

Hello,

I am attempting to scan from an Epson DS-6500 scanner through the feeder, and when I set it to scan all available pages, it returns an error from the Download() method. I'm just using SetProperty(WiaPropertyId.IPS_PAGES, WiaPropertyValue.ALL_PAGES) to set the page value. Is there something different I should be doing?

Message: "Value does not fall within the expected range."
InnerException:null
StackTrace:
at NAPS2.Wia.Native.IWiaTransfer.Download(Int32 lFlags, IWiaTransferCallback pIWiaTransferCallback)
at NAPS2.Wia.Native.NativeWiaMethods.Download2(IntPtr transferPtr, TransferStatusCallback func)
at NAPS2.Wia.WiaTransfer.Download()

Here is the code that does the scanning:

private static List<ScannedImage> scanFromDevice(Device device, bool duplex, int pagesToScan)
{
    List<ScannedImage> images = new List<ScannedImage>();

    // Get device
    using var deviceManager = new WiaDeviceManager(); 
    device.nativeDevice = deviceManager.FindDevice(device.Id);

    // Select "Feeder"
    using var item = device.nativeDevice.FindSubItem("Feeder");

    // Scan all pages in the feeder at once if greater than
    // max pages per scan
    if (pagesToScan > ApplicationConfig.GetMaxPagesPerScan())
        item.SetProperty(WiaPropertyId.IPS_PAGES,
                        WiaPropertyValue.ALL_PAGES);
    else
        item.SetProperty(WiaPropertyId.IPS_PAGES,
                        pagesToScan);
    // Set BW scan
    item.SetProperty(WiaPropertyId.IPA_DATATYPE, 0);
    item.SetProperty(WiaPropertyId.IPS_XRES, 300);
    item.SetProperty(WiaPropertyId.IPS_YRES, 300);

    if (duplex)
    {
        try
        {
            // Enable duplex scanning
            item.SetProperty(WiaPropertyId.IPS_DOCUMENT_HANDLING_SELECT,
                                WiaPropertyValue.DUPLEX);
        }
        catch (System.ArgumentException)
        {
            Console.WriteLine("Duplex scanning not allowed on this machine.");
        }
    }

    // Set up the scan
    using var transfer = item.StartTransfer();
    transfer.PageScanned += (sender, args) =>
    {
        using (args.Stream)
        {
            var bitmap = new System.Drawing.Bitmap(args.Stream);
            // Do something with the image
            string fileName = Path.ChangeExtension(Path.GetTempFileName(), "png");

            File.Delete(fileName);
            bitmap.Save(fileName, ImageFormat.Png);

            // add file to output list
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();

            ScannedImage scannedImage = DrawImage(fileName);
            images.Add(scannedImage);
        }
    };

    // Do the actual scan
    transfer.Download();
    if( images.Count <= 0 )
    {
        Console.WriteLine("No images scanned.");
    }
    return images;
}

Thanks

cyanfish commented 1 month ago

I'm not sure, you can trying comparing against what NAPS2 does or just try using NAPS2.Sdk and see if that resolves the issue.