ARTICLE

Integrate image scanning within a C# application: Part VI

Posted by Bechir Bejaoui Articles | COM Interop November 07, 2008
In this tutorial, I will show a very simple trick of how to integrate the functionality of scanning your documentation with a numeric scanner in your C# application.
Reader Level:
Download Files:
 

Walkthrough

You have to know that this kind of operation is only leveraged within windows XP environment, moreover, the WIA API is not automatically delivered with Windows; therefore, it must be downloaded from this link.

http://www.microsoft.com/downloads/details.aspx?FamilyID=a332a77a-01b8-4de6-91c2-b7ea32537e29&displaylang=en

First, install the WIA API by extracting the content of the zipped file then by copying and pasting the wiaaut.dll assembly to C:\Windows\System32 and finally by registering the assembly using the regsvc32.exe



Figure 1.

Now, include the WIA library within your project by adding a reference to it :



Figure 2.

Add a new class to your project and name it Scanner:

using System;
using WIA;

namespace ScanImage
{
    public class Scanner
    {
        Device oDevice;
        Item oItem;
        CommonDialogClass dlg;
        public Scanner()
        {
         dlg = new CommonDialogClass();
         oDevice = dlg.ShowSelectDevice(WiaDeviceType.ScannerDeviceType, true, false);
        }
        public void Scann()
        {
            dlg.ShowAcquisitionWizard(oDevice);
        }
    }
}

In order to consume the services of this very simple class add a button and a picture box to your form as your project is Windows form one, the form should looks like this bellow:



Figure 3.

Do implement the button1_click as follow:

private void button1_Click(object sender, EventArgs e)
{
    Scanner oScanner = new Scanner();
    oScanner.Scann();
    button1.Text = "Image scanned";
    OpenFileDialog dlg = new OpenFileDialog();
    if (dlg.ShowDialog() == DialogResult.OK)
    {
       pictureBox1.Image = Image.FromFile(dlg.FileName);
    }
}

Now run the application and observe:

Figure 4.

Click OK



Figure 5.

Then the below windows appears :



Figure 6.

Choose the colored photo, and then click next:



Figure 7.

In the above window, select JPG then click next

Figure 8 .

As showed above, the scanning process will be lunched as normally, click next then finish and the paper will be scanned as bellow:

Figure 9

You can browse to the emplacement of the given scanned image and display it using the picture box.

Figure 10

That's it

God Dotneting !!!
 

Login to add your contents and source code to this article
Article Extensions
Contents added by fateme on Jul 20, 2010
post comment
     

from where i got WIA API because i downloaded above Bejaoui4.zip file but it's having only scanner.cs and ButtonClick.cs file and not getting wia api. please suggest me ASAP

Posted by santosh garg Feb 12, 2013

I am using the similar style for WIA, after scanning images into jpeg format i changing them into pdf format. when the changing process exits i want to delete the image file. but the scanning process still using them & protect the from deletion; throws an exceptional error. Is there any way by which i can delete the images and clear the memory.

Posted by suneet saini Sep 18, 2012

CommonDialogClass dlg; dlg = new CommonDialogClass();I am getting an error with "CommonDialogeClass". Error: Interop Type 'WIA.CommonDialogClass' cannot be embeded. Use the aplicable interface instead.What to do now, pls help me out, i am new to c#

Posted by Rupom Ahsan Jun 06, 2012

Hi, This is Rahul. when i use this code for scanning for some file, it throws an error to me i.e. Exception from HRESULT:0x80210015.... please tell me about this. Thanks

Posted by Rahul Sharma Feb 08, 2012

Hi have you found the answer to this yet.... I would also like to know how to do this? Thanks

Posted by Chrispie Smith Feb 03, 2012
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Get Career Advice from Experts