Ejemplo n.º 1
0
        public SaveImagesOperation(FileNamePlaceholders fileNamePlaceholders, ImageSettingsContainer imageSettingsContainer, IOverwritePrompt overwritePrompt, ThreadFactory threadFactory, ScannedImageRenderer scannedImageRenderer, TiffHelper tiffHelper)
        {
            this.fileNamePlaceholders   = fileNamePlaceholders;
            this.imageSettingsContainer = imageSettingsContainer;
            this.overwritePrompt        = overwritePrompt;
            this.threadFactory          = threadFactory;
            this.scannedImageRenderer   = scannedImageRenderer;
            this.tiffHelper             = tiffHelper;

            ProgressTitle = MiscResources.SaveImagesProgress;
            AllowCancel   = true;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves the provided collection of images to a file with the given name. The image type is inferred from the file extension.
        /// If multiple images are provided, they will be saved to files with numeric identifiers, e.g. img1.jpg, img2.jpg, etc..
        /// </summary>
        /// <param name="fileName">The name of the file to save. For multiple images, this is modified by appending a number before the extension.</param>
        /// <param name="dateTime"></param>
        /// <param name="images">The collection of images to save.</param>
        /// <param name="batch"></param>
        public bool Start(string fileName, DateTime dateTime, List <ScannedImage> images, bool batch = false)
        {
            Status = new OperationStatus
            {
                MaxProgress = images.Count
            };
            cancel = false;

            thread = threadFactory.StartThread(() =>
            {
                try
                {
                    var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, batch);
                    if (Directory.Exists(subFileName))
                    {
                        // Not supposed to be a directory, but ok...
                        fileName    = Path.Combine(subFileName, "$(n).jpg");
                        subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, batch);
                    }
                    ImageFormat format = GetImageFormat(subFileName);

                    if (Equals(format, ImageFormat.Tiff))
                    {
                        if (File.Exists(subFileName))
                        {
                            if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes)
                            {
                                return;
                            }
                        }
                        Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName));
                        Status.Success    = TiffHelper.SaveMultipage(images, subFileName, j =>
                        {
                            Status.CurrentProgress = j;
                            InvokeStatusChanged();
                            return(!cancel);
                        });
                        FirstFileSaved = subFileName;
                        return;
                    }

                    int i      = 0;
                    int digits = (int)Math.Floor(Math.Log10(images.Count)) + 1;
                    foreach (ScannedImage img in images)
                    {
                        if (cancel)
                        {
                            return;
                        }
                        Status.CurrentProgress = i;
                        InvokeStatusChanged();

                        if (images.Count == 1 && File.Exists(subFileName))
                        {
                            var dialogResult = overwritePrompt.ConfirmOverwrite(subFileName);
                            if (dialogResult == DialogResult.No)
                            {
                                continue;
                            }
                            if (dialogResult == DialogResult.Cancel)
                            {
                                return;
                            }
                        }
                        using (Bitmap baseImage = img.GetImage())
                        {
                            if (images.Count == 1)
                            {
                                Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName));
                                InvokeStatusChanged();
                                DoSaveImage(baseImage, subFileName, format);
                                FirstFileSaved = subFileName;
                            }
                            else
                            {
                                var fileNameN = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, true, i,
                                                                                            digits);
                                Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(fileNameN));
                                InvokeStatusChanged();
                                DoSaveImage(baseImage, fileNameN, format);

                                if (i == 0)
                                {
                                    FirstFileSaved = fileNameN;
                                }
                            }
                        }
                        i++;
                    }

                    Status.Success = true;
                }
                catch (UnauthorizedAccessException ex)
                {
                    InvokeError(MiscResources.DontHavePermission, ex);
                }
                catch (Exception ex)
                {
                    Log.ErrorException(MiscResources.ErrorSaving, ex);
                    InvokeError(MiscResources.ErrorSaving, ex);
                }
                finally
                {
                    GC.Collect();
                    InvokeFinished();
                }
            });

            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves the provided collection of images to a file with the given name. The image type is inferred from the file extension.
        /// If multiple images are provided, they will be saved to files with numeric identifiers, e.g. img1.jpg, img2.jpg, etc..
        /// </summary>
        /// <param name="fileName">The name of the file to save. For multiple images, this is modified by appending a number before the extension.</param>
        /// <param name="dateTime"></param>
        /// <param name="images">The collection of images to save.</param>
        public void SaveImages(string fileName, DateTime dateTime, ICollection <IScannedImage> images, Func <int, bool> progressCallback)
        {
            try
            {
                var         subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime);
                ImageFormat format      = GetImageFormat(subFileName);

                if (Equals(format, ImageFormat.Tiff))
                {
                    if (File.Exists(subFileName))
                    {
                        if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes)
                        {
                            return;
                        }
                    }
                    Image[] bitmaps = images.Select(x => (Image)x.GetImage()).ToArray();
                    TiffHelper.SaveMultipage(bitmaps, subFileName);
                    foreach (Image bitmap in bitmaps)
                    {
                        bitmap.Dispose();
                    }
                    return;
                }

                int i      = 1;
                int digits = (int)Math.Floor(Math.Log10(images.Count)) + 1;
                foreach (IScannedImage img in images)
                {
                    if (!progressCallback(i))
                    {
                        return;
                    }
                    if (images.Count == 1 && File.Exists(subFileName))
                    {
                        var dialogResult = overwritePrompt.ConfirmOverwrite(subFileName);
                        if (dialogResult == DialogResult.No)
                        {
                            continue;
                        }
                        if (dialogResult == DialogResult.Cancel)
                        {
                            return;
                        }
                    }
                    using (Bitmap baseImage = img.GetImage())
                    {
                        if (images.Count == 1)
                        {
                            DoSaveImage(baseImage, subFileName, format);
                        }
                        else
                        {
                            var fileNameN = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, true, i - 1, digits);
                            DoSaveImage(baseImage, fileNameN, format);
                        }
                    }
                    i++;
                }
            }
            catch (UnauthorizedAccessException)
            {
                errorOutput.DisplayError(MiscResources.DontHavePermission);
            }
            catch (IOException ex)
            {
                Log.ErrorException(MiscResources.ErrorSaving, ex);
                errorOutput.DisplayError(MiscResources.ErrorSaving);
            }
        }