Added WrapAfter option to CombineImages function

This commit is contained in:
Jaex 2022-10-08 12:38:08 +03:00
parent 365c2120ea
commit 6af7de1a22
4 changed files with 92 additions and 61 deletions

View file

@ -2100,31 +2100,101 @@ public static Bitmap LoadImageWithFileDialog(Form form = null)
}
public static Bitmap CombineImages(List<Bitmap> 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 LoadImageWithFileDialog(Form form = null)
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 LoadImageWithFileDialog(Form form = null)
}
public static Bitmap CombineImages(IEnumerable<string> imageFiles, Orientation orientation, ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop,
int space = 0, bool autoFillBackground = false)
int space = 0, int wrapAfter = 0, bool autoFillBackground = false)
{
List<Bitmap> images = new List<Bitmap>();
@ -2203,7 +2231,7 @@ public static Bitmap LoadImageWithFileDialog(Form form = null)
if (images.Count > 1)
{
return CombineImages(images, orientation, alignment, space, autoFillBackground);
return CombineImages(images, orientation, alignment, space, wrapAfter, autoFillBackground);
}
}
finally

View file

@ -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)
{

View file

@ -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;
}
}

View file

@ -833,7 +833,8 @@ public static void CombineImages(IEnumerable<string> 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)
{