public static SaveMultipage ( List |
||
images | List |
|
location | string | |
progressCallback | bool>.Func | |
Résultat | bool |
/// <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 }; var snapshots = images.Select(x => x.Preserve()).ToList(); RunAsync(async() => { 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) && !imageSettingsContainer.ImageSettings.SinglePageTiff) { if (File.Exists(subFileName)) { if (overwritePrompt.ConfirmOverwrite(subFileName) != DialogResult.Yes) { return(false); } } Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)); FirstFileSaved = subFileName; return(await tiffHelper.SaveMultipage(snapshots, subFileName, imageSettingsContainer.ImageSettings.TiffCompression, OnProgress, CancelToken)); } int i = 0; int digits = (int)Math.Floor(Math.Log10(snapshots.Count)) + 1; foreach (ScannedImage.Snapshot snapshot in snapshots) { if (CancelToken.IsCancellationRequested) { return(false); } Status.CurrentProgress = i; InvokeStatusChanged(); if (snapshots.Count == 1 && File.Exists(subFileName)) { var dialogResult = overwritePrompt.ConfirmOverwrite(subFileName); if (dialogResult == DialogResult.No) { continue; } if (dialogResult == DialogResult.Cancel) { return(false); } } if (snapshots.Count == 1) { Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(subFileName)); InvokeStatusChanged(); await DoSaveImage(snapshot, subFileName, format); FirstFileSaved = subFileName; } else { var fileNameN = fileNamePlaceholders.SubstitutePlaceholders(fileName, dateTime, true, i, digits); Status.StatusText = string.Format(MiscResources.SavingFormat, Path.GetFileName(fileNameN)); InvokeStatusChanged(); await DoSaveImage(snapshot, fileNameN, format); if (i == 0) { FirstFileSaved = fileNameN; } } i++; } return(FirstFileSaved != null); } catch (UnauthorizedAccessException ex) { InvokeError(MiscResources.DontHavePermission, ex); } catch (Exception ex) { Log.ErrorException(MiscResources.ErrorSaving, ex); InvokeError(MiscResources.ErrorSaving, ex); } finally { snapshots.ForEach(s => s.Dispose()); GC.Collect(); } return(false); }); Success.ContinueWith(task => { if (task.Result) { Log.Event(EventType.SaveImages, new EventParams { Name = MiscResources.SaveImages, Pages = snapshots.Count, FileFormat = Path.GetExtension(fileName) }); } }, TaskContinuationOptions.OnlyOnRanToCompletion); return(true); }
/// <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); }
/// <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); } }