예제 #1
0
        public ScannedImageSource Scan()
        {
            if (!IsSupported)
            {
                throw new DriverNotSupportedException();
            }
            if (ScanProfile == null)
            {
                throw new InvalidOperationException("IScanDriver.ScanProfile must be specified before calling Scan().");
            }
            if (ScanParams == null)
            {
                throw new InvalidOperationException("IScanDriver.ScanParams must be specified before calling Scan().");
            }
            if (ScanDevice == null)
            {
                throw new InvalidOperationException("IScanDriver.ScanDevice must be specified before calling Scan().");
            }
            if (DialogParent == null && !ScanParams.NoUI)
            {
                throw new InvalidOperationException("IScanDriver.DialogParent must be specified before calling Scan() without NoUI.");
            }

            var source = new ScannedImageSource.Concrete();

            Task.Factory.StartNew(async() =>
            {
                try
                {
                    await ScanInternal(source);
                    source.Done();
                }
                catch (ScanDriverException e)
                {
                    source.Error(e);
                }
                catch (FaultException <ScanDriverExceptionDetail> e)
                {
                    source.Error(e.Detail.Exception);
                }
                catch (Exception e)
                {
                    source.Error(new ScanDriverUnknownException(e));
                }
            }, TaskCreationOptions.LongRunning);
            return(source);
        }
예제 #2
0
        public ScannedImageSource Import(string filePath, ImportParams importParams, ProgressHandler progressCallback, CancellationToken cancelToken)
        {
            var source = new ScannedImageSource.Concrete();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    if (cancelToken.IsCancellationRequested)
                    {
                        source.Done();
                        return;
                    }

                    Bitmap toImport;
                    try
                    {
                        toImport = new Bitmap(filePath);
                    }
                    catch (Exception e)
                    {
                        Log.ErrorException("Error importing image: " + filePath, e);
                        // Handle and notify the user outside the method so that errors importing multiple files can be aggregated
                        throw;
                    }

                    using (toImport)
                    {
                        int frameCount = toImport.GetFrameCount(FrameDimension.Page);
                        int i          = 0;
                        foreach (var frameIndex in importParams.Slice.Indices(frameCount))
                        {
                            progressCallback(i++, frameCount);
                            if (cancelToken.IsCancellationRequested)
                            {
                                source.Done();
                                return;
                            }

                            toImport.SelectActiveFrame(FrameDimension.Page, frameIndex);
                            var image = new ScannedImage(toImport, ScanBitDepth.C24Bit, IsLossless(toImport.RawFormat), -1);
                            if (!importParams.NoThumbnails)
                            {
                                image.SetThumbnail(thumbnailRenderer.RenderThumbnail(toImport));
                            }
                            if (importParams.DetectPatchCodes)
                            {
                                image.PatchCode = PatchCodeDetector.Detect(toImport);
                            }

                            source.Put(image);
                        }

                        progressCallback(frameCount, frameCount);
                    }
                    source.Done();
                }
                catch (Exception e)
                {
                    source.Error(e);
                }
            }, TaskCreationOptions.LongRunning);
            return(source);
        }