Using WIA to get scanner data in C #

Source: Internet
Author: User
Tags visual studio 2010

WIA (Windows Image acquire, latest version 2.0) is a collection of standard APIs for capturing images from devices in Windows, which can obtain still images from devices such as scanners, digital cameras, and manage these devices. It is both an API and DDI (Device Driver Interface). Therefore, as long as the device satisfies this specification, it can use WIA to interact directly with the application, rather than through the driver. WIA even provides a unified dialog box to get pictures.

WIA is COM-based, and there are two ways to use it:

    1. C + +: Using the WIA custom interface
    2. Other: Use Wiaal (WIA Automation Layer).

Note: Wiaal does not exist in previous versions of Windows XP SP1, so the second way is with the WIA Scripting Model.

Using WIA in. NET, we are using the second method. Next, do a simple image scanning program:

Interface

Create a new WinForm application, add a button and a picture frame, click the button to start the scanning process, and then display the image in the picture box, the application interface is as follows:

Using WIA

Visual Studio 2010 has the benefit of automating the assembly of COM components and adding a WIA COM reference to the project:

When you click OK, a WIA.Interop.dll file is added to the project reference, which you can view in the Object Browser:

Open the Scan dialog box

You can then use WIA to scan, and the steps are simple, first referencing namespaces:

using WIA;

Next, in the Click event of the button, add the following code:

ImageFile ImageFile =NULL; Commondialogclass CDC=NewWia.commondialogclass ();Try{ImageFile=CDC. Showacquireimage (WIA). Wiadevicetype.scannerdevicetype, WIA. Wiaimageintent.textintent, WIA. Wiaimagebias.maximizequality,"{00000000-0000-0000-0000-000000000000}",                                     true,                                     true,                                     false);}Catch(System.Runtime.InteropServices.COMException) {ImageFile=NULL;}

WIA will automatically pop up the standard Scan dialog box for scanning operations:

Get images

After the showacquireimage is called, the scanned data is saved in the ImageFile object. The data in the ImageFile is read in the following way (the method is silly and silly ...). Very silly)

if NULL ) {    imagefile.savefile (@ "c:\1.bmp");     using New FileStream (@ "c:\1.bmp", FileMode.Open,        FileAccess.Read, FileShare.Read))    {        = Image.fromstream (stream);    }    File.delete (@ "c:\1.bmp");}

The results are as follows:

Use WIA to get scanner data in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.