Exemple #1
0
        private IScannedImage TransferImage(WiaBackgroundEventLoop eventLoop, int pageNumber)
        {
            try
            {
                using (var stream = wiaTransfer.Transfer(pageNumber, eventLoop, WiaApi.Formats.BMP))
                {
                    if (stream == null)
                    {
                        // User cancelled
                        return(null);
                    }
                    using (Image output = Image.FromStream(stream))
                    {
                        double scaleFactor = 1;
                        if (!ScanSettings.UseNativeUI)
                        {
                            scaleFactor = ScanSettings.AfterScanScale.ToIntScaleFactor();
                        }

                        using (var result = ImageScaleHelper.ScaleImage(output, scaleFactor))
                        {
                            ScanBitDepth bitDepth = ScanSettings.UseNativeUI ? ScanBitDepth.C24Bit : ScanSettings.BitDepth;
                            return(scannedImageFactory.Create(result, bitDepth, ScanSettings.MaxQuality));
                        }
                    }
                }
            }
            catch (COMException e)
            {
                if ((uint)e.ErrorCode == WiaApi.Errors.OUT_OF_PAPER)
                {
                    if (ScanSettings.PaperSource != ScanSource.Glass && pageNumber == 1)
                    {
                        throw new NoPagesException();
                    }
                    return(null);
                }
                else if ((uint)e.ErrorCode == WiaApi.Errors.OFFLINE)
                {
                    throw new DeviceOfflineException();
                }
                else
                {
                    throw new ScanDriverUnknownException(e);
                }
            }
        }
        protected override IEnumerable <IScannedImage> ScanInternal()
        {
            var        session   = new TwainSession(TwainAppId);
            var        twainForm = formFactory.Create <FTwainGui>();
            var        images    = new List <IScannedImage>();
            Exception  error     = null;
            bool       cancel    = false;
            DataSource ds        = null;

            session.TransferReady += (sender, eventArgs) =>
            {
                if (cancel)
                {
                    eventArgs.CancelAll = true;
                }
            };
            session.DataTransferred += (sender, eventArgs) =>
            {
                using (var output = Image.FromStream(eventArgs.GetNativeImageStream()))
                {
                    double scaleFactor = 1;
                    if (!ScanSettings.UseNativeUI)
                    {
                        scaleFactor = ScanSettings.AfterScanScale.ToIntScaleFactor();
                    }

                    using (var result = ImageScaleHelper.ScaleImage(output, scaleFactor))
                    {
                        var bitDepth = output.PixelFormat == PixelFormat.Format1bppIndexed
                            ? ScanBitDepth.BlackWhite
                            : ScanBitDepth.C24Bit;
                        images.Add(scannedImageFactory.Create(result, bitDepth, ScanSettings.MaxQuality));
                    }
                }
            };
            session.TransferError += (sender, eventArgs) =>
            {
                error  = eventArgs.Exception;
                cancel = true;
                twainForm.Close();
            };
            session.SourceDisabled += (sender, eventArgs) => twainForm.Close();

            twainForm.Shown += (sender, eventArgs) =>
            {
                try
                {
                    ReturnCode rc = session.Open(new WindowsFormsMessageLoopHook(DialogParent.Handle));
                    if (rc != ReturnCode.Success)
                    {
                        twainForm.Close();
                        return;
                    }
                    ds = session.FirstOrDefault(x => x.Name == ScanDevice.ID);
                    if (ds == null)
                    {
                        throw new DeviceNotFoundException();
                    }
                    rc = ds.Open();
                    if (rc != ReturnCode.Success)
                    {
                        twainForm.Close();
                        return;
                    }
                    ConfigureDS(ds);
                    var ui = ScanSettings.UseNativeUI ? SourceEnableMode.ShowUI : SourceEnableMode.NoUI;
                    rc = ds.Enable(ui, true, twainForm.Handle);
                    if (rc != ReturnCode.Success)
                    {
                        twainForm.Close();
                    }
                }
                catch (Exception ex)
                {
                    error = ex;
                    twainForm.Close();
                }
            };

            twainForm.ShowDialog(DialogParent);

            if (ds != null && session.IsSourceOpen)
            {
                ds.Close();
            }
            if (session.IsDsmOpen)
            {
                session.Close();
            }

            if (error != null)
            {
                if (error is ScanDriverException)
                {
                    throw error;
                }
                throw new ScanDriverUnknownException(error);
            }

            return(images);
        }