Getting a Windows Image Acquisition Scan error in C#

54 Views Asked by At

Une erreur s'est produite lors de la numérisation :
System.Runtime.InteropServices.COMException (0x8021006B) 0x8021006B

at WIA.DeviceInfos Class.getItem/object& Index)
at AppScanner.NumberOfDocument.button4_Click(Object sender, EventArgs e) in C: Zineb Application AppScanner AppScanner Form1.cs:line 543

This is line 543:

Device scanner = (Device)manager.DeviceInfos[0];

Here is the button code that must scan the documents from our scanner (Epson):

private async void button4_Click(object sender, EventArgs e)
{
        try
        {
            DeviceManager manager = new DeviceManager();

            if (manager.DeviceInfos.Count > 0)
            {
                Device scanner = (Device)manager.DeviceInfos[0];

                // Vérifier que le scanner est connecté
                if (scanner != null)
                {
                    // Afficher les propriétés du scanner
                    foreach (Property prop in scanner.Properties)
                    {
                        MessageBox.Show($"{prop.Name}: {prop.get_Value()}");
                    }

                    // Configurer les propriétés du scanner
                    scanner.Properties["ADF Duplex"].set_Value(true);
                    scanner.Properties["Resolution"].set_Value(300);

                    // Numériser le document
                    Item item = scanner.Items[1]; // Assurez-vous que l'index 1 est valide
                    ImageFile imageFile = (ImageFile)item.Transfer();
                    // ...
                }
                else
                {
                    MessageBox.Show("Aucun scanner WIA n'a été trouvé.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("Aucun périphérique WIA n'a été trouvé.", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show($"Une erreur s'est produite lors de la numérisation : {ex.ToString()}", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}

Knowing that the documents will be displayed in a PDF viewer (pdfuimvier1) that I use in my application here is the display code :

public NumberOfDocument()
{
        InitializeComponent();
        connectionString = ConfigurationManager.ConnectionStrings["MaConnexion"].ConnectionString;

        //button4.Click += button4_Click;
        // Initialize the form
        this.Text = "Scan Document";
        this.Size = new Size(800, 600);
        // Create the scan button
        this.button4 = new Button();
        this.button4.Text = "Scan";
        this.button4.Location = new System.Drawing.Point(100, 100);
        this.button4.Size = new Size(100, 30);
        this.button4.Click += new EventHandler(this.button4_Click);
        // Create the PDF viewer
        this.pdfViewer1 = new PdfViewer();
        this.pdfViewer1.Location = new System.Drawing.Point(100, 200);
        this.pdfViewer1.Size = new Size(600, 400);
        // Add the controls to the form
        this.Controls.Add(this.button4);
        this.Controls.Add(this.pdfViewer1);
}
0

There are 0 best solutions below