Using C#/WIA version 2.0 on Vista to Scan
Asked Answered
I

5

23

I want to implement a paperless filing system and was looking to use WIA with C# for the image acquisition. There are quite a few sample projects on CodeProject, etc. However, after downloading every one of them that I can find, I have run into a problem.

In each and every one of them, the reference to WIALib is broken. When I go to add "Microsoft Windows Image Acquisition" as a reference, the only version available on my development workstation (also the machine that will run this) is 2.0.

Unfortunately, every one of these sample projects appear to have been coded against 1.x. The reference goes in as "WIA" instead of "WIALib". I took a shot, just changing the namespace import, but clearly the API is drastically different.

Is there any information on either implementing v2.0 or on upgrading one of these existing sample projects out there?

Isoline answered 12/8, 2008 at 15:28 Comment(1)
Quick question. Do you absolutely need WIA? Or can you get by with Twain? If Twain is OK I might have some code to donate.Winne
T
22

To access WIA, you'll need to add a reference to the COM library, "Microsoft Windows Image Acquisition Library v2.0" (wiaaut.dll). add a "using WIA;"

const string wiaFormatJPEG = "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}";
CommonDialogClass wiaDiag = new CommonDialogClass();
WIA.ImageFile wiaImage = null;

wiaImage = wiaDiag.ShowAcquireImage(
        WiaDeviceType.UnspecifiedDeviceType, 
        WiaImageIntent.GrayscaleIntent, 
        WiaImageBias.MaximizeQuality, 
        wiaFormatJPEG, true, true, false);

WIA.Vector vector = wiaImage.FileData;

(System.Drawing)

Image i = Image.FromStream(new MemoryStream((byte[])vector.get_BinaryData()));
i.Save(filename)

Thats a basic way, works with my flatbed/doc feeder. If you need more than one document/page at a time though, there is probably a better way to do it (from what I could see, this only handles one image at a time, although I'm not entirely sure). While it is a WIA v1 doc, Scott Hanselman's Coding4Fun article on WIA does contain some more info on how to do it for multiple pages, I think (I'm yet to go further than that myself)

If its for a paperless office system, you might want also check out MODI (Office Document Imaging) to do all the OCR for you.

Trotyl answered 20/9, 2008 at 11:28 Comment(4)
I'll give this a shot. I"m not messing with OCR because I haven't seen any of the systems get anywhere close enough on my stuff to be more useful than just doing some decent metadata and tagging. This should give me most of what I need as once it's referenced, I can dig through the API.Isoline
Instead of that magic GUID you can use System.Drawing.Imaging.ImageFormat.Jpeg.Guid.ToString("B").Discrimination
Or you can use the guid defined in Interop.WIA.dll. FormatID.wiaFormatJPEGStringy
The code above will throw an exception with the message "Exception from HRESULT: 0x80210015" if there aren't any valid WIA devices available. You can check for devices using: WIA.DeviceManagerClass wiaDM = new DeviceManagerClass(); if (wiaDM == null || wiaDM.DeviceInfos == null || wiaDM.DeviceInfos.Count == 0) // No devicesBakery
R
1

Heres how to target WIA 1.0 also so you can ship your app to Windows Xp. Something I was desperately looking for!! How to develop using WIA 1 under Vista?

Rasheedarasher answered 23/7, 2010 at 18:13 Comment(0)
R
1

Update: I'm adding this separately since its a different answer (a year later). I learnt XP has WIA 1.0 and Vista onward has WIA2.0. You can however install WIA 2.0 for Windows XP Sp1+ from here.

I then also made a small library with code I found somewhere on the interweb here, it also has the ability to scan multiple pages: http://adfwia.codeplex.com/

Rasheedarasher answered 28/8, 2011 at 10:38 Comment(0)
I
0

It doesn't need to be WIA. I was mostly looking at the WIA setup because it offers the same basic interface for different scanners. I've got 3 scanners on this machine and the TWAIN drivers/software for all of them suck (like blocking the screen during scanning).

For document management, I'm really looking for simple 200dpi grayscale scans, so most of the stuff in the TWAIN drivers is overkill.

That said, asking here was part of my last attempt to figure out how to do it in WIA before moving on to TWAIN.

Isoline answered 12/8, 2008 at 16:28 Comment(0)
M
0

Another note: You have to download the WIA 2.0 dll from Microsoft.com and then browse to the dll and add it to your project.

Marcy answered 28/9, 2008 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.