Code refactoring

This commit is contained in:
Jaex 2022-10-08 10:24:57 +03:00
parent e4f0fce3f0
commit 365c2120ea
3 changed files with 28 additions and 26 deletions

View file

@ -2099,30 +2099,31 @@ public static Bitmap LoadImageWithFileDialog(Form form = null)
return null; return null;
} }
public static Bitmap CombineImages(List<Bitmap> images, Orientation orientation = Orientation.Vertical, public static Bitmap CombineImages(List<Bitmap> images, Orientation orientation, ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop,
ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop, int space = 0, bool autoFillBackground = false) int space = 0, bool autoFillBackground = false)
{ {
int width, height; int width, height;
int imageCount = images.Count; int imageCount = images.Count;
int spaceSize = space * (imageCount - 1); int spaceSize = space * (imageCount - 1);
if (orientation == Orientation.Vertical) if (orientation == Orientation.Horizontal)
{
width = images.Max(x => x.Width);
height = images.Sum(x => x.Height) + spaceSize;
}
else
{ {
width = images.Sum(x => x.Width) + spaceSize; width = images.Sum(x => x.Width) + spaceSize;
height = images.Max(x => x.Height); height = images.Max(x => x.Height);
} }
else
{
width = images.Max(x => x.Width);
height = images.Sum(x => x.Height) + spaceSize;
}
Bitmap bmp = new Bitmap(width, height); Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp)) using (Graphics g = Graphics.FromImage(bmp))
{ {
g.SetHighQuality(); g.SetHighQuality();
int position = 0;
Point position = new Point(0, 0);
for (int i = 0; i < imageCount; i++) for (int i = 0; i < imageCount; i++)
{ {
@ -2135,44 +2136,45 @@ public static Bitmap LoadImageWithFileDialog(Form form = null)
} }
Rectangle rect; Rectangle rect;
Point offset = new Point(0, 0);
if (orientation == Orientation.Vertical) if (orientation == Orientation.Horizontal)
{ {
int x;
switch (alignment) switch (alignment)
{ {
default: default:
case ImageCombinerAlignment.LeftOrTop: case ImageCombinerAlignment.LeftOrTop:
x = 0; offset.Y = 0;
break; break;
case ImageCombinerAlignment.Center: case ImageCombinerAlignment.Center:
x = (width / 2) - (image.Width / 2); offset.Y = (height / 2) - (image.Height / 2);
break; break;
case ImageCombinerAlignment.RightOrBottom: case ImageCombinerAlignment.RightOrBottom:
x = width - image.Width; offset.Y = height - image.Height;
break; break;
} }
rect = new Rectangle(x, position, image.Width, image.Height);
position += image.Height + space; rect = new Rectangle(position.X + offset.X, position.Y + offset.Y, image.Width, image.Height);
position.X += image.Width + space;
} }
else else
{ {
int y;
switch (alignment) switch (alignment)
{ {
default: default:
case ImageCombinerAlignment.LeftOrTop: case ImageCombinerAlignment.LeftOrTop:
y = 0; offset.X = 0;
break; break;
case ImageCombinerAlignment.Center: case ImageCombinerAlignment.Center:
y = (height / 2) - (image.Height / 2); offset.X = (width / 2) - (image.Width / 2);
break; break;
case ImageCombinerAlignment.RightOrBottom: case ImageCombinerAlignment.RightOrBottom:
y = height - image.Height; offset.X = width - image.Width;
break; break;
} }
rect = new Rectangle(position, y, image.Width, image.Height);
position += image.Width + space; 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, rect);
@ -2182,8 +2184,8 @@ public static Bitmap LoadImageWithFileDialog(Form form = null)
return bmp; return bmp;
} }
public static Bitmap CombineImages(IEnumerable<string> imageFiles, Orientation orientation = Orientation.Vertical, public static Bitmap CombineImages(IEnumerable<string> imageFiles, Orientation orientation, ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop,
ImageCombinerAlignment alignment = ImageCombinerAlignment.LeftOrTop, int space = 0, bool autoFillBackground = false) int space = 0, bool autoFillBackground = false)
{ {
List<Bitmap> images = new List<Bitmap>(); List<Bitmap> images = new List<Bitmap>();

View file

@ -33,6 +33,6 @@ public class ImageCombinerOptions
public Orientation Orientation { get; set; } = Orientation.Vertical; public Orientation Orientation { get; set; } = Orientation.Vertical;
public ImageCombinerAlignment Alignment { get; set; } = ImageCombinerAlignment.LeftOrTop; public ImageCombinerAlignment Alignment { get; set; } = ImageCombinerAlignment.LeftOrTop;
public int Space { get; set; } = 0; public int Space { get; set; } = 0;
public bool AutoFillBackground { get; set; } = false; public bool AutoFillBackground { get; set; } = true;
} }
} }

View file

@ -608,7 +608,7 @@ private Bitmap CombineImages()
output.Add(newImage); output.Add(newImage);
} }
Bitmap bmpResult = ImageHelpers.CombineImages(output); Bitmap bmpResult = ImageHelpers.CombineImages(output, Orientation.Vertical);
foreach (Bitmap image in output) foreach (Bitmap image in output)
{ {