|
|||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Disclaimer: The information in this article & source code are published in accordance with the final [V1] bits of the .NET Framework AbstractThis 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! WIAWIA 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.
The API is exposed as COM interfaces, in two flavors:
A seamless and low-effort integration with .NET is (currently) only possible with WIA Scripting. WIA ScriptingThe WIA Scripting Model is described in the WIA Scripting reference on MSDN Platform SDK. The typical steps needed to acquire an image are as follows:
With pseudo code this looks as simple as: manager = new Wia
root = manager.Create
collection = root.GetItemsFromUI
collection[0..n].Transfer
WIA provides its own common dialogs to select a device:
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 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 root e.g. Camera Device
+root.Children
item1
item2 e.g. Folder
+item2.Children
item21 e.g. Picture1
item22 e.g. Picture2
CodeTo use WIA Scripting in your Visual Studio .NET project, add a reference to the component "Microsoft Windows Image Acquisition 1.01 Type Library" (wiascr.dll)
Without VS.NET, you will have to use the TLBIMP tool. Now you can add the WIA Scripting namespace at the top of your C# code file: using System.IO;
using System.Runtime.InteropServices;
// namespace of imported WIA Scripting COM component
using WIALib;
This imported library namespace provides mapping of the WIA Scripting types to .NET wrapper classes:
and now you can write code like this simplified sample to acquire pictures: // create COM instance of WIA manager
wiaManager = new WiaClass();
object selectUsingUI = System.Reflection.Missing.Value;
// let user select device
wiaRoot = (ItemClass) wiaManager.Create(
ref selectUsingUI );
// this call shows the common WIA dialog to let
// the user select a picture:
wiaPics = wiaRoot.GetItemsFromUI( WiaFlag.SingleImage,
WiaIntent.ImageTypeColor ) as CollectionClass;
// enumerate all the pictures the user selected
foreach( object wiaObj in wiaPics )
{
wiaItem = (ItemClass) Marshal.CreateWrapperOfType(
wiaObj, typeof(ItemClass) );
// create temporary file for image
imageFileName = Path.GetTempFileName();
// transfer picture to our temporary file
wiaItem.Transfer( imageFileName, false );
}
For more information on COM interop and WIA debugging read these two articles: Asynchronous Transfer and EventsSome 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 // asynchronously transfer picture to file
wiaItem.Transfer( imageFileName, true );
To notify the application about the completed transfer and more, the WIA manager exposes three events:
These events nicely map on to the .NET event/delegate model, so we add a handler function like so: // delegate member variable
private _IWiaEvents_OnTransferCompleteEventHandler
wiaEvtTransfer;
...
// create event delegate
wiaEvtTransfer = new _IWiaEvents_OnTransferCompleteEventHandler(
this.wia_OnTransferComplete );
// subscribe to event
wiaManager.OnTransferComplete += wiaEvtTransfer;
...
// event handler function (callback from WIA!)
public void wia_OnTransferComplete(
WIALib.Item item, string path )
{
... // logic to handle completed transfer
}
VideoFor web cams or other video devices, WIA on Windows XP has a great new feature: live video stream overlay! It uses The code to show a real-time and live video stream in a window could be as simple as these two steps: wiaVideo = new WiaVideoClass();
wiaVideo.CreateVideoByWiaDevID(
wiaDeviceID, window.Handle, 0, 1 );
and to take a snapshot jpeg image from the current video stream is possible with one single method: // this string will get the filename of the picture...
string jpgFile;
// call IWiaVideo::TakePicture
wiaVideo.TakePicture( out jpgFile );
Check the included video sample for more! SamplesFull source code with Visual Studio .NET projects are provided for these samples:
Limitations
|
||||||||||||||||||||||||||||||