diff --git a/ShareX.HelpersLib/Helpers/ImageHelpers.cs b/ShareX.HelpersLib/Helpers/ImageHelpers.cs index 6acfc364b..f98b9653c 100644 --- a/ShareX.HelpersLib/Helpers/ImageHelpers.cs +++ b/ShareX.HelpersLib/Helpers/ImageHelpers.cs @@ -2100,31 +2100,101 @@ public static Bitmap LoadImageWithFileDialog(Form form = null) } public static Bitmap CombineImages(List images, Orientation orientation, ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop, - int space = 0, bool autoFillBackground = false) + int space = 0, int wrapAfter = 0, bool autoFillBackground = false) { - int width, height; int imageCount = images.Count; - int spaceSize = space * (imageCount - 1); + Rectangle[] imageRects = new Rectangle[imageCount]; + Point position = new Point(0, 0); + int currentSize = 0; - if (orientation == Orientation.Horizontal) + for (int i = 0; i < imageCount; i++) { - width = images.Sum(x => x.Width) + spaceSize; - height = images.Max(x => x.Height); - } - else - { - width = images.Max(x => x.Width); - height = images.Sum(x => x.Height) + spaceSize; + Bitmap image = images[i]; + Point offset = new Point(0, 0); + + if (orientation == Orientation.Horizontal) + { + if (wrapAfter > 0) + { + if (i % wrapAfter == 0) + { + if (i > 0) + { + position.X = 0; + position.Y += currentSize + space; + } + + currentSize = images.Skip(i).Take(wrapAfter).Max(x => x.Height); + } + } + else if (i == 0) + { + currentSize = images.Max(x => x.Height); + } + + switch (alignment) + { + default: + case ImageCombinerAlignment.LeftOrTop: + offset.Y = 0; + break; + case ImageCombinerAlignment.Center: + offset.Y = (currentSize / 2) - (image.Height / 2); + break; + case ImageCombinerAlignment.RightOrBottom: + offset.Y = currentSize - image.Height; + break; + } + + imageRects[i] = new Rectangle(position.X + offset.X, position.Y + offset.Y, image.Width, image.Height); + position.X += image.Width + space; + } + else + { + if (wrapAfter > 0) + { + if (i % wrapAfter == 0) + { + if (i > 0) + { + position.X += currentSize + space; + position.Y = 0; + } + + currentSize = images.Skip(i).Take(wrapAfter).Max(x => x.Width); + } + } + else if (i == 0) + { + currentSize = images.Max(x => x.Width); + } + + switch (alignment) + { + default: + case ImageCombinerAlignment.LeftOrTop: + offset.X = 0; + break; + case ImageCombinerAlignment.Center: + offset.X = (currentSize / 2) - (image.Width / 2); + break; + case ImageCombinerAlignment.RightOrBottom: + offset.X = currentSize - image.Width; + break; + } + + imageRects[i] = new Rectangle(position.X + offset.X, position.Y + offset.Y, image.Width, image.Height); + position.Y += image.Height + space; + } } - Bitmap bmp = new Bitmap(width, height); + Rectangle totalImageRect = imageRects.Combine(); + Bitmap bmp = new Bitmap(totalImageRect.Width, totalImageRect.Height); using (Graphics g = Graphics.FromImage(bmp)) { g.SetHighQuality(); - Point position = new Point(0, 0); - for (int i = 0; i < imageCount; i++) { Bitmap image = images[i]; @@ -2135,49 +2205,7 @@ public static Bitmap CombineImages(List images, Orientation orientation, g.Clear(backgroundColor); } - Rectangle rect; - Point offset = new Point(0, 0); - - if (orientation == Orientation.Horizontal) - { - switch (alignment) - { - default: - case ImageCombinerAlignment.LeftOrTop: - offset.Y = 0; - break; - case ImageCombinerAlignment.Center: - offset.Y = (height / 2) - (image.Height / 2); - break; - case ImageCombinerAlignment.RightOrBottom: - offset.Y = height - image.Height; - break; - } - - rect = new Rectangle(position.X + offset.X, position.Y + offset.Y, image.Width, image.Height); - position.X += image.Width + space; - } - else - { - switch (alignment) - { - default: - case ImageCombinerAlignment.LeftOrTop: - offset.X = 0; - break; - case ImageCombinerAlignment.Center: - offset.X = (width / 2) - (image.Width / 2); - break; - case ImageCombinerAlignment.RightOrBottom: - offset.X = width - image.Width; - break; - } - - rect = new Rectangle(position.X + offset.X, position.Y + offset.Y, image.Width, image.Height); - position.Y += image.Height + space; - } - - g.DrawImage(image, rect); + g.DrawImage(image, imageRects[i]); } } @@ -2185,7 +2213,7 @@ public static Bitmap CombineImages(List images, Orientation orientation, } public static Bitmap CombineImages(IEnumerable imageFiles, Orientation orientation, ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop, - int space = 0, bool autoFillBackground = false) + int space = 0, int wrapAfter = 0, bool autoFillBackground = false) { List images = new List(); @@ -2203,7 +2231,7 @@ public static Bitmap CombineImages(IEnumerable imageFiles, Orientation o if (images.Count > 1) { - return CombineImages(images, orientation, alignment, space, autoFillBackground); + return CombineImages(images, orientation, alignment, space, wrapAfter, autoFillBackground); } } finally diff --git a/ShareX.MediaLib/Forms/ImageCombinerForm.cs b/ShareX.MediaLib/Forms/ImageCombinerForm.cs index 62c8d8fa9..009b5ba75 100644 --- a/ShareX.MediaLib/Forms/ImageCombinerForm.cs +++ b/ShareX.MediaLib/Forms/ImageCombinerForm.cs @@ -180,7 +180,8 @@ private void btnCombine_Click(object sender, EventArgs e) if (imageFiles.Count > 1) { - Bitmap output = ImageHelpers.CombineImages(imageFiles, Options.Orientation, Options.Alignment, Options.Space, Options.AutoFillBackground); + Bitmap output = ImageHelpers.CombineImages(imageFiles, Options.Orientation, Options.Alignment, Options.Space, Options.WrapAfter, + Options.AutoFillBackground); if (output != null) { diff --git a/ShareX.MediaLib/ImageCombinerOptions.cs b/ShareX.MediaLib/ImageCombinerOptions.cs index 8d77a8eb5..717805680 100644 --- a/ShareX.MediaLib/ImageCombinerOptions.cs +++ b/ShareX.MediaLib/ImageCombinerOptions.cs @@ -33,6 +33,7 @@ public class ImageCombinerOptions public Orientation Orientation { get; set; } = Orientation.Vertical; public ImageCombinerAlignment Alignment { get; set; } = ImageCombinerAlignment.LeftOrTop; public int Space { get; set; } = 0; + public int WrapAfter { get; set; } = 0; public bool AutoFillBackground { get; set; } = true; } } \ No newline at end of file diff --git a/ShareX/TaskHelpers.cs b/ShareX/TaskHelpers.cs index d63bf8430..81e378549 100644 --- a/ShareX/TaskHelpers.cs +++ b/ShareX/TaskHelpers.cs @@ -833,7 +833,8 @@ public static void CombineImages(IEnumerable imageFiles, Orientation ori if (taskSettings == null) taskSettings = TaskSettings.GetDefaultTaskSettings(); Bitmap output = ImageHelpers.CombineImages(imageFiles, orientation, taskSettings.ToolsSettings.ImageCombinerOptions.Alignment, - taskSettings.ToolsSettings.ImageCombinerOptions.Space, taskSettings.ToolsSettings.ImageCombinerOptions.AutoFillBackground); + taskSettings.ToolsSettings.ImageCombinerOptions.Space, taskSettings.ToolsSettings.ImageCombinerOptions.WrapAfter, + taskSettings.ToolsSettings.ImageCombinerOptions.AutoFillBackground); if (output != null) {