我试图在我的WPF应用程序,Caliburn Micro和Windows.Device.Scanners中使用送纸器扫描仪扫描文件。该应用程序在第一次扫描时工作正常,但是当我关闭视图并重新打开时,Visual Studio给了我错误"检测到断开连接的上下文"扫描完毕后。
if (!IsScanning)
{
ImageScannerScanResult result=null;
IsScanning = true;
result= await myScanner.ScanFilesToFolderAsync(ImageScannerScanSource.Feeder, folder).AsTask(cancellationToken.Token, progress);
if (result.ScannedFiles.Count > 0)
{
try
{
StorageFile file = result.ScannedFiles[0];
System.Drawing.Bitmap Bit = new System.Drawing.Bitmap(file.Path);
Execute.OnUIThread(() =>
{
_Image = Utils.BitmapToBitmapSource(Bit);
NotifyOfPropertyChange(() => Image);
});
IsScanning = false;
}
catch(Exception e)
{
IsScanning = false;
}
这是OnDeactivate()
protected override void OnDeactivate(bool close)
{
base.OnDeactivate(close);
_CurrentScannerDeviceId = null;
if (scannerWatcher != null)
{
scannerWatcher.Added -= OnScannerAdded;
scannerWatcher.Removed -= OnScannerRemoved;
scannerWatcher.EnumerationCompleted -= OnScannerEnumerationComplete;
if (scannerWatcher.Status == DeviceWatcherStatus.Started)
{
scannerWatcher.Stop();
}
scannerWatcher = null;
}
if(cancellationToken!=null)
cancellationToken.Cancel();
GC.Collect();
}
答案 0 :(得分:0)
您似乎正在使用WIA方法进行扫描。我宁愿建议使用TWAIN API(在我们公司,我们尝试过两种方式,而且看起来更好)。 有几个.NET twain包装器库: