From d7aa0ab7f5f69189745a1480a811da5334d37625 Mon Sep 17 00:00:00 2001 From: Jaex Date: Mon, 14 Jun 2021 15:18:09 +0300 Subject: [PATCH] Added CombineImages method --- ShareX.HelpersLib/Helpers/ImageHelpers.cs | 48 +++++++++++++++++++--- ShareX.MediaLib/Forms/ImageCombinerForm.cs | 41 ++++-------------- ShareX/TaskHelpers.cs | 17 +++++++- ShareX/UploadInfoManager.cs | 2 +- 4 files changed, 66 insertions(+), 42 deletions(-) diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index 0c38af817..7bd7d155f 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -1817,9 +1817,9 @@ public static string SaveImageFileDialog(Image img, string filePath = "", bool u public static Bitmap LoadImage(string filePath) { - try + if (!string.IsNullOrEmpty(filePath)) { - if (!string.IsNullOrEmpty(filePath)) + try { filePath = Helpers.GetAbsolutePath(filePath); @@ -1836,10 +1836,10 @@ public static Bitmap LoadImage(string filePath) return bmp; } } - } - catch (Exception e) - { - DebugHelper.WriteException(e); + catch (Exception e) + { + DebugHelper.WriteException(e); + } } return null; @@ -1940,6 +1940,42 @@ public static Bitmap LoadImageWithFileDialog() return bmp; } + public static Bitmap CombineImages(IEnumerable imageFiles, Orientation orientation = Orientation.Vertical, + ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop, int space = 0, bool autoFillBackground = false) + { + List images = new List(); + + try + { + foreach (string filePath in imageFiles) + { + Bitmap bmp = LoadImage(filePath); + + if (bmp != null) + { + images.Add(bmp); + } + } + + if (images.Count > 1) + { + return CombineImages(images, orientation, alignment, space, autoFillBackground); + } + } + finally + { + foreach (Bitmap bmp in images) + { + if (bmp != null) + { + bmp.Dispose(); + } + } + } + + return null; + } + public static List SplitImage(Image img, int rowCount, int columnCount) { List images = new List(); diff --git a/ShareX.MediaLib/Forms/ImageCombinerForm.cs b/ShareX.MediaLib/Forms/ImageCombinerForm.cs index 9c4fa2f6b..6aee6bce0 100644 --- a/ShareX.MediaLib/Forms/ImageCombinerForm.cs +++ b/ShareX.MediaLib/Forms/ImageCombinerForm.cs @@ -28,7 +28,7 @@ using System; using System.Collections.Generic; using System.Drawing; -using System.IO; +using System.Linq; using System.Windows.Forms; namespace ShareX.MediaLib @@ -174,50 +174,25 @@ private void btnCombine_Click(object sender, EventArgs e) { if (lvImages.Items.Count > 0) { - List images = new List(); - try { - foreach (ListViewItem lvi in lvImages.Items) - { - string filePath = lvi.Text; + List imageFiles = lvImages.Items.Cast().Select(x => x.Text).ToList(); - if (File.Exists(filePath)) + if (imageFiles.Count > 1) + { + Bitmap output = ImageHelpers.CombineImages(imageFiles, Options.Orientation, Options.Alignment, Options.Space, Options.AutoFillBackground); + + if (output != null) { - Bitmap bmp = ImageHelpers.LoadImage(filePath); - - if (bmp != null) - { - images.Add(bmp); - } + OnProcessRequested(output); } } - - if (images.Count > 1) - { - Bitmap output = ImageHelpers.CombineImages(images, Options.Orientation, Options.Alignment, Options.Space, Options.AutoFillBackground); - - OnProcessRequested(output); - } } catch (Exception ex) { DebugHelper.WriteException(ex); ex.ShowError(); } - finally - { - if (images != null) - { - foreach (Bitmap image in images) - { - if (image != null) - { - image.Dispose(); - } - } - } - } } } diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index cd659817e..de158aff6 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -212,7 +212,7 @@ public static void ExecuteJob(TaskSettings taskSettings, HotkeyType job, CLIComm UploadManager.IndexFolder(); break; case HotkeyType.ImageCombiner: - OpenImageCombiner(safeTaskSettings); + OpenImageCombiner(null, safeTaskSettings); break; case HotkeyType.ImageSplitter: OpenImageSplitter(); @@ -841,7 +841,7 @@ public static void OpenDirectoryIndexer(TaskSettings taskSettings = null) form.Show(); } - public static void OpenImageCombiner(TaskSettings taskSettings = null, IEnumerable imageFiles = null) + public static void OpenImageCombiner(IEnumerable imageFiles = null, TaskSettings taskSettings = null) { if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings(); @@ -850,6 +850,19 @@ public static void OpenImageCombiner(TaskSettings taskSettings = null, IEnumerab imageCombinerForm.Show(); } + public static void CombineImages(IEnumerable imageFiles, Orientation orientation, TaskSettings taskSettings = null) + { + if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings(); + + Bitmap output = ImageHelpers.CombineImages(imageFiles, orientation, taskSettings.ToolsSettings.ImageCombinerOptions.Alignment, + taskSettings.ToolsSettings.ImageCombinerOptions.Space, taskSettings.ToolsSettings.ImageCombinerOptions.AutoFillBackground); + + if (output != null) + { + UploadManager.RunImageTask(output, taskSettings); + } + } + public static void OpenImageSplitter() { ImageSplitterForm imageSplitterForm = new ImageSplitterForm(); diff --git a/ShareX/UploadInfoManager.cs b/ShareX/UploadInfoManager.cs index ed72da8d9..2059c7198 100644 --- a/ShareX/UploadInfoManager.cs +++ b/ShareX/UploadInfoManager.cs @@ -394,7 +394,7 @@ public void CombineImages() if (imageFiles.Count() > 1) { - TaskHelpers.OpenImageCombiner(null, imageFiles); + TaskHelpers.OpenImageCombiner(imageFiles); } } }