我正在使用saraff twain库。扫描程序在向客户端扫描过程时应返回异常。因此,例如,如果扫描仪的进纸器一次拉出两页(或更多),它将返回异常。但是,当我尝试使用库时,无法捕获这些异常。
这是我的代码:
public Stream Acquire(Source source, IEnumerable<CapEnum> caps)
{
try
{
var _formatter = new BinaryFormatter();
var _result = new MemoryStream();
var _images = new List<System.Drawing.Image>();
TwainExternalProcess.Execute(
TwainService._GetExecFileName(source),
twain =>
{
twain.EndXfer += (sender, e) => _images.Add(e.Image);
twain.SourceIndex = source.Id;
twain.OpenDataSource();
foreach (var _cap in caps)
{
using (var _stream = new MemoryStream(_cap.Current.RawValue))
{
twain.SetCap(_cap.Cap, _formatter.Deserialize(_stream));
}
}
twain.Acquire();
});
var _params = new EncoderParameters(2);
_params.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionNone);
_params.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
_images[0].Save(
_result, ImageCodecInfo.GetImageEncoders().First(x => x.MimeType == "image/tiff"), _params);
_params.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
for (int i = 0; i < _images.Count; i++)
{
_images[0].SaveAdd(_images[i], _params);
}
foreach (var _image in _images)
{
_image.Dispose();
}
_result.Seek(0, SeekOrigin.Begin);
return _result;
}catch(TwainException twainException)
{
throw twainException;
}
catch (Exception ex)
{
throw new FaultException(ex.Message);
}
}
我希望该方法在PaperJam(双纸进纸)的情况下应该引发异常。但是,我无法捕获到异常。
您能告诉我我在想什么吗?我正在使用佳能DR-C240扫描仪。