English 中文(简体)
Why can t WIA see my scanner?
原标题:

I m trying to use WIA (Microsoft Windows Image Acquisition Library v2.0) to build a C# 3.5 WinForms app in VS2008 running on a Vista rig to aquire images from a scanner.

I know there are plenty of SDKs out there that do this (Accusoft, ByteScout, Knowledge Lake, etc) but we wanted some control over the UI (or lack of) and the ability to customize the processing and handling of the images, which is why we re trying the WIA angle.

However, I have been unable to get WIA to see my scanner.

The Microsoft Windows Image Acquisition Library v2.0 dll has been referenced in the VS project and I have included using WIA; at the top of the page.

Here is the section of code:

//Choose Scanner
CommonDialogClass class1 = new CommonDialogClass();
Device d = class1.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);
if (d != null)
{
    this.DeviceID = d.DeviceID;
}
else
{
    //no scanner chosen
    return;
}

Complies fine but line 2 (Device d = ...) kicks the following error when run:
Exception from HRESULT: 0x80210015

From what I can tell, this usually means your device is unplugged/not turned on or the device is not WIA compatible.
But the scanner in question shows up in Control Panel/Scanners and Cameras (means it s WIA compatible) and works when accessed via Photoshop (means it s turned on).

I have plugged in other devices (Digital SLR) and the above code can see them, so the code is working.

Does anyone have any suggestions as to what is going wrong and how to fix it?

Update 1:
I have tried a couple of different scanners (Canon 5000F, Benq 5250C), but same problem.

Update 2:
I have been unable to find definitive proof of this, but I m thinking that the scanners I have been testing with, or maybe most scanners :( , are not WIA compatible/supported. I m am now looking into using TWAIN, but would still love to hear of anyone who has had some success with WIA.

Update 3: Ended up ditching WIA and using a .NET Twain SDK (EZTwain). All sorted now. Thanks to everyone for thier input.

问题回答

I think your scanner doesn t support WIA. I recommend to use TWAIN which is supported by most vendors.

I recommend to use NTWAIN library:

Nuget Pakcage: https://www.nuget.org/packages/NTwain/

Source Code: https://bitbucket.org/soukoku/ntwain

Have a look at this article on CodeProject that covers TWAIN. This might help you in that direction, also, here is another article about WIA, despite it is a bit old but worth a look nonetheless.

I would try the following code when connected only your scanner

IDeviceManager dm = ClassFactory.createDeviceManager();
System.out.println(dm.deviceInfos().count());

if the device manager can see your scanner at all

wiaImage = wiaDiag.ShowAcquireImage(WiaDeviceType.ScannerDeviceType, WiaImageIntent.GrayscaleIntent, WiaImageBias.MaximizeQuality, wiaFormatJPEG, true, true, false);
WIA.Vector vector = wiaImage.FileData;
FileExtention = wiaImage.FileExtension;
Image i = Image.FromStream(new MemoryStream((byte[])vector.get_BinaryData()));
pbPreview1.Image = i;//to show preview of scanned image in picturebox
ICommonDialog dialog = new CommonDialog();
Device device = dialog.ShowSelectDevice(WiaDeviceType.UnspecifiedDeviceType, true, false);

As an alternative.

This is my way to test WIA scanner:

private bool WIAScannerTest() 
{
       try
       {
           WIA.CommonDialog wiaObj = New WIA.CommonDialog(); 
           WIA.Device wiaDev = 
                  wiaObj.ShowSelectDevice(WIA.WiaDeviceType.ScannerDeviceType, false, false);
           return true;
       }
       catch (Exception ex)
       {}
       finally
       {
           if(wiaDev != null)
           {
               Marshal.ReleaseComObject(wiaDev)
               wiaDev = null;
           }
           if(wiaObj != null)
           {
              Marshal.ReleaseComObject(wiaObj)
              wiaObj = null;
           }
       }
       return false;
}

Most likely, no WIA drivers are installed for the devices. I would suggest you should use TWAIN instead, which is supported by almost all vendors.

There are quite a number of open source as well as commercially TWAIN wrappers. Most of them work for 32bit applications and 64bit applications fail to access 32bit TWAIN drivers. If you need to support both 64bit and 32bit WinForms WPF application, you may consider to use Asprise C# VB.NET scanning for twain wia scanner.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签