Disclaimer: The information in this article & source code are published in accordance with the final [V1] bits of the .NET Framework

Abstract

This sample shows how to use Windows XP Windows Image Acquisition (WIA) Scripting with .NET and C#. It is useful for integrating with scanners, digital cameras, webcams and still-video.

Note, this article doesn't save you from reading the detailed WIA documentation!

WIA

WIA is a standardized Win32 API for acquiring digital images from devices that are primarily used to capture still images, and for managing these devices. WIA was introduced with Windows Millennium and updated for Windows XP. Today, most digital imaging devices are supported on XP by built-in or manufacturer provided WIA drivers.

and e.g. dialogs specific to a scanner device, ... or specific to a photo camera device:

Note, some tasks are also possible without user (GUI) interaction. One such method is Item.TakePicture but you should check the documentation for a complete list.

The WIA Scripting Model organizes all known items (devices, folders, pictures,...) in a hierarchical structure, e.g.: WIA Camera Tree. An advanced application can recursively enumerate this tree using the Item.Children property:

Asynchronous Transfer and Events

Some devices, especially photo-cameras on serial COM ports, are slow! So it will take many seconds to transfer pictures. WIA Scripting solves this issue with an asynchronous flag when using Item.Transfer:

Video

For web cams or other video devices, WIA on Windows XP has a great new feature: live video stream overlay! It uses DirectShow to draw the overlay on the graphics card. The IWiaVideo interface can be accessed by importing another COM type library: "WiaVideo 1.0 Type Library" (wiavideo.dll). Unfortunately, the embedded TLB has a bug for methods passing a window handle. I used ILDASM to get the IL code of the interop assembly, then I changed all incorrect occurances of 'valuetype _RemotableHandle&' to 'native int', then finally compiled back to an assembly with ILASM. This repaired DLL is included in the download as WIAVIDEOLib.dll.

The code to show a real-time and live video stream in a window could be as simple as these two steps:

WHEN I DEBUG THIS PROJECT IN VISUAL STUDIO 2010 THEN IT GENERATES AN ERROR AND THE ERROR MESSAGE IS THAT WIALIB.DLL FILES IS NOT REGISTERED (THE TYPE OR NAMESPACE WIALIB COULDNOT FOUND U R MISSING A ASSEMBLY REFERENCE OR USING DIRECTIVE) WHERE I GET THIS DLL FILES . AND HOW I REGISTER IT IN SYSTEM 32 FOLDER (I M USING 32 BIT VERSION OF WINDOW)

WHEN I DEBUG THIS PROJECT IN VISUAL STUDIO 2010 THEN IT GENERATES AN ERROR AND THE ERROR MESSAGE IS THAT WIALIB.DLL FILES IS NOT REGISTERED (THE TYPE OR NAMESPACE WIALIB COULDNOT FOUND U R MISSING A ASSEMBLY REFERENCE OR USING DIRECTIVE) WHERE I GET THIS DLL FILES . AND HOW I REGISTER IT IN SYSTEM 32 FOLDER (I M USING 32 BIT VERSION OF WINDOW)

I know it's been a long time since this article was published, but here's hoping that the author is still around, or someone can address this.

I downloaded the WiaEasyVideo and converted it for use with Visual Studio C# 2008 Express Version. It compiles without error, but when I run it an exception gets thrown from within the CreateCamera method:

The exception gets thrown while executing the above statement, where it's trying to make use of the "Type" member of the devInfo object. As far as I can tell the failure actually occurs in the immediately preceding statement where it's attempting to convert the wiaObj object into the devInfo object.

The problem is that it does the release of the wiaObj too soon. It apparently needs to hold on to that until you're done with BOTH the wiaObj AND the devInfo object, since they seem to be intimately related.

The WIA scripting application will not build if running visual studio on windows 7 (I guess the same applies to Vista). To run and build the application you need to utilise visual studio on Windows XP. This application uses WIAv1.0. For Windows 7 you need to use WIAv2.0

The WIA scripting application will not build if running visual studio on windows 7 (I guess the same applies to Vista). To run and build the application you need to utilise visual studio on Windows XP.

I implemented this code in my project to acquire images, is working perfectly. However, this code will only work on Win XP. I could not make the code work on Windows 7, some classes of COM do not work in Win 7. If possible could someone help me how to make this code work on Windows 7?