'Scanning all documents in feeder and working with ImageFile

I want to scan all documents in a feeder and save each Image in a List of Images.

I have the following Setup:

  1. A C# Console Application with .NET6.0
  2. Added Reference to Microsoft Windows Image Acquisition Library v2.0
  3. A Fujitsu fi-6130 Scanning-Device (feeder only)

My Code inside the static Main-Method consists of:

    var dialog = new CommonDialogClass();
    var device = dialog.ShowSelectDevice();
    // set Pages to 0 => all documents get scanned
    device.Properties["Pages"].set_Value(0);

    var item = device.Items[1];

    var imageFile = (IImageFile)item.Transfer(FormatID.wiaFormatTIFF);

Now, when I execute this Code, the following error occurs:

System.IO.FileNotFoundException: "Das System kann die angegebene Datei nicht finden. (0x80070002)"

In English, basically saying that the file was not found.

In this post's answer, it is implied that Item.Transfer() uses a Temporary File to store a transferred Image, so this might be an angle to tackle the problem. Hence I added the following Code:

    string path = Path.GetTempFileName();
    // clear temp file
    File.Delete(path);
    imageFile.SaveFile(path+".tiff");

which of course doesn't execute, because the error occurs when Item.Transfer() (see above) is called.

In the given example, the solution basically was to call the Transfer-Method as long as there are documents in the feeder, which is not my preferred was of doing this, as it throws an error when your feeder is emptied.

How do I proceed from here? Do I have to fall back on the iterative method? Does WIA.Vector play into this at all? (Just a theory based on this Blogpost)

Best regards.



Solution 1:[1]

This works although it gets sketchy with the Exception-Handling. And with WIA, there are lots of Exceptions!

ICommonDialog dialog = new CommonDialogClass();
IDevice device = dialog.ShowSelectDevice();

IItem item = device.Items[1];

var images = new List<Image>();

try
{ 
    while (true)
    {
        IImageFile image = (IImageFile)item.Transfer(FormatID.wiaFormatTIFF);
        
        string path = Path.GetTempFileName();
        File.Delete(path);
        image.SaveFile($"{path}");

        images.Add(Image.FromFile(path));

    }
}
catch(Exception exc) { WriteLine(exc.ToString()); }

int i = 0;
foreach(var image in images)
{
    image.Save($"image{i++}.tiff");
}

Inside the Main-Method of course.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Halil Ibrahim Özcan