示例#1
0
 private IScannedImage ExportJpegImage(PdfPage page, byte[] imageBytes)
 {
     // Fortunately JPEG has native support in PDF and exporting an image is just writing the stream to a file.
     using (var memoryStream = new MemoryStream(imageBytes))
     {
         using (var bitmap = new Bitmap(memoryStream))
         {
             bitmap.SetResolution(bitmap.Width / (float)page.Width.Inch, bitmap.Height / (float)page.Height.Inch);
             return(scannedImageFactory.Create(bitmap, ScanBitDepth.C24Bit, false));
         }
     }
 }
示例#2
0
        private IScannedImage MakeImage()
        {
            var bitmap = new Bitmap(600, 800);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.FillRectangle(Brushes.LightGray, 0, 0, bitmap.Width, bitmap.Height);
                g.DrawString(new Random().Next().ToString("G"), new Font("Times New Roman", 80), Brushes.Black, 0, 350);
            }
            var image = scannedImageFactory.Create(bitmap, ScanBitDepth.C24Bit, ScanSettings.MaxQuality);

            return(image);
        }
示例#3
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);
                }
            }
        }
示例#4
0
 private IEnumerable <IScannedImage> DoRecover()
 {
     foreach (RecoveryIndexImage indexImage in recoveryIndexManager.Index.Images)
     {
         string imagePath = Path.Combine(folderToRecoverFrom.FullName, indexImage.FileName);
         using (var bitmap = new Bitmap(imagePath))
         {
             var scannedImage = scannedImageFactory.Create(bitmap, indexImage.BitDepth, indexImage.HighQuality);
             foreach (var transform in indexImage.TransformList)
             {
                 scannedImage.AddTransform(transform);
             }
             scannedImage.SetThumbnail(scannedImage.RenderThumbnail(userConfigManager.Config.ThumbnailSize));
             yield return(scannedImage);
         }
     }
 }
示例#5
0
        public IEnumerable <IScannedImage> Import(string filePath)
        {
            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)
            {
                for (int i = 0; i < toImport.GetFrameCount(FrameDimension.Page); ++i)
                {
                    toImport.SelectActiveFrame(FrameDimension.Page, i);
                    yield return(scannedImageFactory.Create(toImport, ScanBitDepth.C24Bit, IsLossless(toImport.RawFormat)));
                }
            }
        }
示例#6
0
        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);
        }