它是关于 TWAINdotNet 库的。我花了过去 24 小时尝试在 C#/WPF/VS2010 中进行基本的虚拟扫描仪实验。我已经从http://sourceforge.net/projects/twain-samples/files/下载并安装了虚拟扫描仪驱动程序,它在可用的 TWAIN 数据源列表中显示得很好。有趣的是,此页面上提供的示例应用程序也可以与此驱动程序完美配合。仅当我尝试通过 C#/WPF 使用它时才会出现此问题。
具体来说,调用DsUserInterface
usingMessage.EnableDS
失败,天知道是什么原因。无论我是否要求它显示 UI,它在所有情况下都会失败。同样DsImageLayout
,在尝试设置扫描区域时调用也会导致失败。最后但并非最不重要的一点是,尝试将测量单位设置为英寸(或其他)也不会生效。调用不会失败,但之前的值保持不变。
这是代码,以防我可能做错了什么:
public partial class MainWindow : Window
{
TwainDotNet.Wpf.WpfWindowMessageHook mTwnHook;
TwainDotNet.Twain mTWN;
TwainDotNet.ScanSettings setting = new TwainDotNet.ScanSettings();
public MainWindow()
{
InitializeComponent();
}
private void btnScan_Click(object sender, RoutedEventArgs e)
{
InitializeTWAIN();
mTWN.ScanningComplete += twn_ScanningComplete;
mTWN.TransferImage += twn_TransferImage;
mTWN.StartScanning(setting);
}
private void InitializeTWAIN()
{
mTwnHook = new TwainDotNet.Wpf.WpfWindowMessageHook(this);
mTWN = new TwainDotNet.Twain(mTwnHook);
setting.UseAutoFeeder = true;
setting.UseDocumentFeeder = true;
setting.UseDuplex = true;
setting.TransferCount = 1;
setting.Page = new TwainDotNet.PageSettings()
{
Orientation = TwainDotNet.TwainNative.Orientation.Auto,
Size = TwainDotNet.TwainNative.PageType.UsLetter
};
setting.Area = new TwainDotNet.AreaSettings(TwainDotNet.TwainNative.Units.Millimeters, 0f, 0f, 279.4f, 215.9f);
setting.Resolution = new TwainDotNet.ResolutionSettings()
{
ColourSetting = TwainDotNet.ColourSetting.Colour,
Dpi = 100
};
setting.ShouldTransferAllPages = true;
setting.ShowTwainUI = false;
}
void twn_TransferImage(object sender, TwainDotNet.TransferImageEventArgs e)
{
//save image
}
void twn_ScanningComplete(object sender, TwainDotNet.ScanningCompleteEventArgs e)
{
if (e.Exception != null)
MessageBox.Show(e.Exception.Message);
}
}