示例#1
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;
        }
示例#2
0
        public bool Start(List<string> filesToImport, Action<IScannedImage> imageCallback)
        {
            bool oneFile = filesToImport.Count == 1;
            Status = new OperationStatus
            {
                MaxProgress = oneFile ? 0 : filesToImport.Count
            };
            cancel = false;

            thread = threadFactory.StartThread(() =>
            {
                foreach (var fileName in filesToImport)
                {
                    try
                    {
                        Status.StatusText = string.Format(MiscResources.ImportingFormat, Path.GetFileName(fileName));
                        InvokeStatusChanged();
                        var images = scannedImageImporter.Import(fileName, (i, j) =>
                        {
                            if (oneFile)
                            {
                                Status.CurrentProgress = i;
                                Status.MaxProgress = j;
                                InvokeStatusChanged();
                            }
                            return !cancel;
                        });
                        foreach (var img in images)
                        {
                            imageCallback(img);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.ErrorException(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(fileName)), ex);
                        InvokeError(string.Format(MiscResources.ImportErrorCouldNot, Path.GetFileName(fileName)));
                    }
                    if (!oneFile)
                    {
                        Status.CurrentProgress++;
                        InvokeStatusChanged();
                    }
                }
                Status.Success = true;
                InvokeFinished();
            });
            return true;
        }
示例#3
0
        public bool Start(List<string> filesToImport, Action<ScannedImage> imageCallback)
        {
            bool oneFile = filesToImport.Count == 1;
            Status = new OperationStatus
            {
                MaxProgress = oneFile ? 0 : filesToImport.Count
            };
            cancel = false;

            thread = threadFactory.StartThread(() =>
            {
                Run(filesToImport, imageCallback, oneFile);
                GC.Collect();
                InvokeFinished();
            });
            return true;
        }
示例#4
0
        public bool Start(string fileName, DateTime dateTime, ICollection<ScannedImage> images, PdfSettings pdfSettings, string ocrLanguageCode, bool email)
        {
            ProgressTitle = email ? MiscResources.EmailPdfProgress : MiscResources.SavePdfProgress;
            var subFileName = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime);
            Status = new OperationStatus
            {
                StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)),
                MaxProgress = images.Count
            };
            cancel = false;

            if (File.Exists(subFileName))
            {
                if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes)
                {
                    return false;
                }
            }

            thread = threadFactory.StartThread(() =>
            {
                try
                {
                    Status.Success = pdfExporter.Export(subFileName, images, pdfSettings, ocrLanguageCode, i =>
                    {
                        Status.CurrentProgress = i;
                        InvokeStatusChanged();
                        return !cancel;
                    });
                }
                catch (UnauthorizedAccessException)
                {
                    InvokeError(MiscResources.DontHavePermission);
                }
                catch (Exception ex)
                {
                    Log.ErrorException(MiscResources.ErrorSaving, ex);
                    InvokeError(MiscResources.ErrorSaving);
                }
                InvokeFinished();
            });

            return true;
        }
示例#5
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 = FirstFileSaved != null;
                }
                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;
        }
示例#6
0
            public bool Start(Action<IScannedImage> imageCallback)
            {
                Status = new OperationStatus
                {
                    StatusText = MiscResources.Recovering
                };
                cancel = false;

                folderToRecoverFrom = FindAndLockFolderToRecoverFrom();
                if (folderToRecoverFrom == null)
                {
                    return false;
                }
                try
                {
                    recoveryIndexManager = new RecoveryIndexManager(folderToRecoverFrom);
                    imageCount = recoveryIndexManager.Index.Images.Count;
                    scannedDateTime = folderToRecoverFrom.LastWriteTime;
                    if (imageCount == 0)
                    {
                        // If there are no images, do nothing. Don't delete the folder in case the index was corrupted somehow.
                        ReleaseFolderLock();
                        return false;
                    }
                    switch (PromptToRecover())
                    {
                        case DialogResult.Yes: // Recover
                            threadFactory.StartThread(() =>
                            {
                                try
                                {
                                    if (DoRecover(imageCallback))
                                    {
                                        ReleaseFolderLock();
                                        DeleteFolder();
                                        Status.Success = true;
                                    }
                                }
                                finally
                                {
                                    ReleaseFolderLock();
                                    InvokeFinished();
                                }
                            });
                            return true;
                        case DialogResult.No: // Delete
                            ReleaseFolderLock();
                            DeleteFolder();
                            break;
                        default: // Not Now
                            ReleaseFolderLock();
                            break;
                    }
                }
                catch (Exception)
                {
                    ReleaseFolderLock();
                    throw;
                }
                return false;
            }