扫描图像文件时检测到断开连接的上下文
本文关键字:断开 连接 上下文 检测 图像 文件 扫描 | 更新日期: 2023-09-27 18:34:51
>我正在尝试在我的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();
}
看起来您正在使用 WIA 方法进行扫描。我宁愿建议使用 TWAIN API(在我们公司,我们尝试了两种方式,吐温看起来更好(。有几个 .NET 吐温包装库:
- 萨拉夫吐温
- TwainDotNet(作为NuGet包提供(
- NTwain(作为NuGet包提供(