SetThumbnail() public method

public SetThumbnail ( Bitmap bitmap ) : void
bitmap System.Drawing.Bitmap
return void
Esempio n. 1
0
 public IEnumerable<ScannedImage> Import(string filePath, Func<int, int, bool> progressCallback)
 {
     if (!progressCallback(0, 1))
     {
         yield break;
     }
     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);
         for (int i = 0; i < frameCount; ++i)
         {
             if (!progressCallback(i, frameCount))
             {
                 yield break;
             }
             toImport.SelectActiveFrame(FrameDimension.Page, i);
             var image = new ScannedImage(toImport, ScanBitDepth.C24Bit, IsLossless(toImport.RawFormat), -1);
             image.SetThumbnail(thumbnailRenderer.RenderThumbnail(toImport));
             yield return image;
         }
         progressCallback(frameCount, frameCount);
     }
 }
Esempio n. 2
0
        private static void AddTransformAndUpdateThumbnail(ScannedImage image, Transform transform)
        {
            image.AddTransform(transform);
            var thumbnail = image.GetThumbnail(null);

            if (thumbnail != null)
            {
                image.SetThumbnail(transform.Perform(thumbnail));
            }
        }
Esempio n. 3
0
        private void AddTransformAndUpdateThumbnail(ScannedImage image, ref Bitmap bitmap, Transform transform)
        {
            image.AddTransform(transform);
            var thumbnail = image.GetThumbnail(null);

            if (thumbnail != null)
            {
                bitmap = transform.Perform(bitmap);
                image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
            }
        }
Esempio n. 4
0
        public bool Start(DirectImageTransfer data, bool copy, Action<ScannedImage> imageCallback)
        {
            ProgressTitle = copy ? MiscResources.CopyProgress : MiscResources.ImportProgress;
            Status = new OperationStatus
            {
                StatusText = copy ? MiscResources.Copying : MiscResources.Importing,
                MaxProgress = data.ImageRecovery.Length
            };
            cancel = false;

            thread = threadFactory.StartThread(() =>
            {
                Exception error = null;
                foreach (var ir in data.ImageRecovery)
                {
                    try
                    {
                        ScannedImage img;
                        using (var bitmap = new Bitmap(Path.Combine(data.RecoveryFolder, ir.FileName)))
                        {
                            img = new ScannedImage(bitmap, ir.BitDepth, ir.HighQuality, -1);
                        }
                        foreach (var transform in ir.TransformList)
                        {
                            img.AddTransform(transform);
                        }
                        img.SetThumbnail(thumbnailRenderer.RenderThumbnail(img));
                        imageCallback(img);

                        Status.CurrentProgress++;
                        InvokeStatusChanged();
                        if (cancel)
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        error = ex;
                    }
                }
                if (error != null)
                {
                    Log.ErrorException(string.Format(MiscResources.ImportErrorCouldNot, data.RecoveryFolder), error);
                }
                Status.Success = true;
                InvokeFinished();
            });
            return true;
        }
Esempio n. 5
0
 public void PostProcessStep2(ScannedImage image, Bitmap bitmap, ScanProfile profile, ScanParams scanParams, int pageNumber, bool supportsNativeUI = true)
 {
     if (!scanParams.NoThumbnails)
     {
         image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
     }
     if (scanParams.SkipPostProcessing)
     {
         return;
     }
     if (profile.StretchHistogram && !profile.HistogramStretchConfig.IsNull)
     {
         AddTransformAndUpdateThumbnail(image, ref bitmap, new StretchHistogramTransform {
             Parameters = profile.HistogramStretchConfig
         });
     }
     if ((!profile.UseNativeUI || !supportsNativeUI) && profile.BrightnessContrastAfterScan)
     {
         if (profile.Brightness != 0)
         {
             AddTransformAndUpdateThumbnail(image, ref bitmap, new BrightnessTransform {
                 Brightness = profile.Brightness
             });
         }
         if (profile.Contrast != 0)
         {
             AddTransformAndUpdateThumbnail(image, ref bitmap, new TrueContrastTransform {
                 Contrast = profile.Contrast
             });
         }
     }
     if (profile.FlipDuplexedPages && pageNumber % 2 == 0)
     {
         AddTransformAndUpdateThumbnail(image, ref bitmap, new RotationTransform(RotateFlipType.Rotate180FlipNone));
     }
     if (profile.AutoDeskew)
     {
         var op = operationFactory.Create <DeskewOperation>();
         if (op.Start(new[] { image }))
         {
             operationProgress.ShowProgress(op);
             op.Wait();
         }
     }
     if (scanParams.DetectPatchCodes && image.PatchCode == PatchCode.None)
     {
         image.PatchCode = PatchCodeDetector.Detect(bitmap);
     }
 }
Esempio n. 6
0
 private static void AddTransformAndUpdateThumbnail(ScannedImage image, Transform transform)
 {
     image.AddTransform(transform);
     var thumbnail = image.GetThumbnail(null);
     if (thumbnail != null)
     {
         image.SetThumbnail(transform.Perform(thumbnail));
     }
 }
Esempio n. 7
0
 private ScannedImage TransferImage(WiaBackgroundEventLoop eventLoop, int pageNumber, out bool cancel)
 {
     try
     {
         var transfer = ScanParams.NoUI ? backgroundWiaTransfer : foregroundWiaTransfer;
         ChaosMonkey.MaybeError(0.5, new COMException("Fail", -2147467259));
         using (var stream = transfer.Transfer(pageNumber, eventLoop, WiaApi.Formats.BMP))
         {
             if (stream == null)
             {
                 cancel = true;
                 return null;
             }
             cancel = false;
             using (Image output = Image.FromStream(stream))
             {
                 using (var result = ScannedImageHelper.PostProcessStep1(output, ScanProfile))
                 {
                     if (blankDetector.ExcludePage(result, ScanProfile))
                     {
                         return null;
                     }
                     ScanBitDepth bitDepth = ScanProfile.UseNativeUI ? ScanBitDepth.C24Bit : ScanProfile.BitDepth;
                     var image = new ScannedImage(result, bitDepth, ScanProfile.MaxQuality, ScanProfile.Quality);
                     image.SetThumbnail(thumbnailRenderer.RenderThumbnail(result));
                     ScannedImageHelper.PostProcessStep2(image, result, ScanProfile, ScanParams, pageNumber);
                     return image;
                 }
             }
         }
     }
     catch (NoPagesException)
     {
         if (ScanProfile.PaperSource != ScanSource.Glass && pageNumber == 1)
         {
             // No pages were in the feeder, so show the user an error
             throw new NoPagesException();
         }
         // At least one page was scanned but now the feeder is empty, so exit normally
         cancel = true;
         return null;
     }
     catch (ScanDriverException)
     {
         throw;
     }
     catch (Exception e)
     {
         throw new ScanDriverUnknownException(e);
     }
 }
Esempio n. 8
0
            private bool DoRecover(Action<ScannedImage> imageCallback)
            {
                Status.MaxProgress = recoveryIndexManager.Index.Images.Count;
                InvokeStatusChanged();

                foreach (RecoveryIndexImage indexImage in recoveryIndexManager.Index.Images)
                {
                    if (cancel)
                    {
                        return false;
                    }

                    string imagePath = Path.Combine(folderToRecoverFrom.FullName, indexImage.FileName);
                    ScannedImage scannedImage;
                    using (var bitmap = new Bitmap(imagePath))
                    {
                        scannedImage = new ScannedImage(bitmap, indexImage.BitDepth, indexImage.HighQuality, -1);
                    }
                    foreach (var transform in indexImage.TransformList)
                    {
                        scannedImage.AddTransform(transform);
                    }
                    scannedImage.SetThumbnail(thumbnailRenderer.RenderThumbnail(scannedImage));
                    imageCallback(scannedImage);

                    Status.CurrentProgress++;
                    InvokeStatusChanged();
                }
                return true;
            }
Esempio n. 9
0
        public List<ScannedImage> Scan(IWin32Window dialogParent, bool activate, ScanDevice scanDevice, ScanProfile scanProfile, ScanParams scanParams)
        {
            if (scanProfile.TwainImpl == TwainImpl.Legacy)
            {
                return Legacy.TwainApi.Scan(scanProfile, scanDevice, dialogParent, formFactory);
            }

            PlatformInfo.Current.PreferNewDSM = scanProfile.TwainImpl != TwainImpl.OldDsm;
            var session = new TwainSession(TwainAppId);
            var twainForm = formFactory.Create<FTwainGui>();
            var images = new List<ScannedImage>();
            Exception error = null;
            bool cancel = false;
            DataSource ds = null;

            int pageNumber = 0;

            session.TransferReady += (sender, eventArgs) =>
            {
                Debug.WriteLine("NAPS2.TW - TransferReady");
                if (cancel)
                {
                    eventArgs.CancelAll = true;
                }
            };
            session.DataTransferred += (sender, eventArgs) =>
            {
                Debug.WriteLine("NAPS2.TW - DataTransferred");
                pageNumber++;
                using (var output = Image.FromStream(eventArgs.GetNativeImageStream()))
                {
                    using (var result = ScannedImageHelper.PostProcessStep1(output, scanProfile))
                    {
                        if (blankDetector.ExcludePage(result, scanProfile))
                        {
                            return;
                        }

                        var bitDepth = output.PixelFormat == PixelFormat.Format1bppIndexed
                            ? ScanBitDepth.BlackWhite
                            : ScanBitDepth.C24Bit;
                        var image = new ScannedImage(result, bitDepth, scanProfile.MaxQuality, scanProfile.Quality);
                        image.SetThumbnail(thumbnailRenderer.RenderThumbnail(result));
                        if (scanParams.DetectPatchCodes)
                        {
                            foreach (var patchCodeInfo in eventArgs.GetExtImageInfo(ExtendedImageInfo.PatchCode))
                            {
                                if (patchCodeInfo.ReturnCode == ReturnCode.Success)
                                {
                                    image.PatchCode = GetPatchCode(patchCodeInfo);
                                }
                            }
                        }
                        ScannedImageHelper.PostProcessStep2(image, result, scanProfile, scanParams, pageNumber);
                        images.Add(image);
                    }
                }
            };
            session.TransferError += (sender, eventArgs) =>
            {
                Debug.WriteLine("NAPS2.TW - TransferError");
                if (eventArgs.Exception != null)
                {
                    error = eventArgs.Exception;
                }
                else if (eventArgs.SourceStatus != null)
                {
                    Log.Error("TWAIN Transfer Error. Return code = {0}; condition code = {1}; data = {2}.",
                        eventArgs.ReturnCode, eventArgs.SourceStatus.ConditionCode, eventArgs.SourceStatus.Data);
                }
                else
                {
                    Log.Error("TWAIN Transfer Error. Return code = {0}.", eventArgs.ReturnCode);
                }
                cancel = true;
                twainForm.Close();
            };
            session.SourceDisabled += (sender, eventArgs) =>
            {
                Debug.WriteLine("NAPS2.TW - SourceDisabled");
                twainForm.Close();
            };

            twainForm.Shown += (sender, eventArgs) =>
            {
                if (activate)
                {
                    // TODO: Set this flag based on whether NAPS2 already has focus
                    // http://stackoverflow.com/questions/7162834/determine-if-current-application-is-activated-has-focus
                    // Or maybe http://stackoverflow.com/questions/156046/show-a-form-without-stealing-focus
                    twainForm.Activate();
                }
                Debug.WriteLine("NAPS2.TW - TwainForm.Shown");
                try
                {
                    ReturnCode rc = session.Open(new WindowsFormsMessageLoopHook(dialogParent.Handle));
                    if (rc != ReturnCode.Success)
                    {
                        Debug.WriteLine("NAPS2.TW - Could not open session - {0}", rc);
                        twainForm.Close();
                        return;
                    }
                    ds = session.FirstOrDefault(x => x.Name == scanDevice.ID);
                    if (ds == null)
                    {
                        Debug.WriteLine("NAPS2.TW - Could not find DS - DS count = {0}", session.Count());
                        throw new DeviceNotFoundException();
                    }
                    rc = ds.Open();
                    if (rc != ReturnCode.Success)
                    {
                        Debug.WriteLine("NAPS2.TW - Could not open DS - {0}", rc);
                        twainForm.Close();
                        return;
                    }
                    ConfigureDS(ds, scanProfile, scanParams);
                    var ui = scanProfile.UseNativeUI ? SourceEnableMode.ShowUI : SourceEnableMode.NoUI;
                    Debug.WriteLine("NAPS2.TW - Enabling DS");
                    rc = ds.Enable(ui, true, twainForm.Handle);
                    Debug.WriteLine("NAPS2.TW - Enable finished");
                    if (rc != ReturnCode.Success)
                    {
                        Debug.WriteLine("NAPS2.TW - Enable failed - {0}, rc");
                        twainForm.Close();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("NAPS2.TW - Error");
                    error = ex;
                    twainForm.Close();
                }
            };

            Debug.WriteLine("NAPS2.TW - Showing TwainForm");
            twainForm.ShowDialog(dialogParent);
            Debug.WriteLine("NAPS2.TW - TwainForm closed");

            if (ds != null && session.IsSourceOpen)
            {
                Debug.WriteLine("NAPS2.TW - Closing DS");
                ds.Close();
            }
            if (session.IsDsmOpen)
            {
                Debug.WriteLine("NAPS2.TW - Closing session");
                session.Close();
            }

            if (error != null)
            {
                Debug.WriteLine("NAPS2.TW - Throwing error - {0}", error);
                if (error is ScanDriverException)
                {
                    throw error;
                }
                throw new ScanDriverUnknownException(error);
            }

            return images;
        }
Esempio n. 10
0
 private ScannedImage 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);
             var image = new ScannedImage(bitmap, ScanBitDepth.C24Bit, false, -1);
             image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
             return image;
         }
     }
 }
Esempio n. 11
0
        private ScannedImage ExportAsPngImage(PdfPage page, PdfDictionary imageObject)
        {
            int width = imageObject.Elements.GetInteger(PdfImage.Keys.Width);
            int height = imageObject.Elements.GetInteger(PdfImage.Keys.Height);
            int bitsPerComponent = imageObject.Elements.GetInteger(PdfImage.Keys.BitsPerComponent);

            var buffer = imageObject.Stream.UnfilteredValue;

            Bitmap bitmap;
            ScanBitDepth bitDepth;
            switch (bitsPerComponent)
            {
                case 8:
                    bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                    bitDepth = ScanBitDepth.C24Bit;
                    RgbToBitmapUnmanaged(height, width, bitmap, buffer);
                    break;
                case 1:
                    bitmap = new Bitmap(width, height, PixelFormat.Format1bppIndexed);
                    bitDepth = ScanBitDepth.BlackWhite;
                    BlackAndWhiteToBitmapUnmanaged(height, width, bitmap, buffer);
                    break;
                default:
                    throw new NotImplementedException("Unsupported image encoding (expected 24 bpp or 1bpp)");
            }

            using (bitmap)
            {
                bitmap.SetResolution(bitmap.Width / (float)page.Width.Inch, bitmap.Height / (float)page.Height.Inch);
                var image = new ScannedImage(bitmap, bitDepth, true, -1);
                image.SetThumbnail(thumbnailRenderer.RenderThumbnail(bitmap));
                return image;
            }
        }