这是关于TWAINdotNet库。我花了24小时尝试在C#/ WPF / VS2010中进行基本的虚拟扫描程序实验。我已经从http://sourceforge.net/projects/twain-samples/files/下载并安装了虚拟扫描仪驱动程序,它在可用的TWAIN数据源列表中显示得很好。有趣的是,此页面上提供的示例应用程序也适用于此驱动程序。只有当我尝试通过C#/ WPF使用它时才会出现问题。
具体来说,使用DsUserInterface
调用Message.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);
}
}