How to Merge Images of Different Sizes with Alignment and Padding
Merging photos or scans of different sizes can lead to ugly overlaps or gaps. Aspose.Imaging for .NET lets you control alignment and padding, creating polished, uniform collages from any source images.
Real-World Problem
Images for merging are often of varying width and height—such as camera uploads, scans, or sourced from different devices. Simple merges look misaligned or unprofessional without proper handling.
Solution Overview
Programmatically calculate maximum dimensions, add padding, and align each image (top, center, bottom, or left, center, right) in the composite for a seamless, professional look.
Prerequisites
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.Imaging for .NET from NuGet
- Folder of images to merge (mixed sizes allowed)
PM> Install-Package Aspose.Imaging
Step-by-Step Implementation
Step 1: Organize Images and Set Merge/Alignment Options
bool mergeHorizontal = true; // or false for vertical
string alignment = "center"; // options: "top", "center", "bottom" for horizontal; "left", "center", "right" for vertical
string[] files = Directory.GetFiles("./input", "*.jpg");
Step 2: Load Images and Calculate Output Size
var images = files.Select(f => Image.Load(f)).ToList();
int maxWidth = images.Max(img => img.Width);
int maxHeight = images.Max(img => img.Height);
int totalWidth = mergeHorizontal ? images.Sum(img => img.Width) : maxWidth;
int totalHeight = mergeHorizontal ? maxHeight : images.Sum(img => img.Height);
int padding = 20; // px between images
Step 3: Create Canvas and Draw with Alignment & Padding
using (var outImg = Image.Create(new PngOptions(), totalWidth + (mergeHorizontal ? padding * (images.Count - 1) : 0), totalHeight + (mergeHorizontal ? 0 : padding * (images.Count - 1))))
{
var graphics = new Aspose.Imaging.Graphics(outImg);
graphics.Clear(Color.White); // Set background color
int x = 0, y = 0;
foreach (var img in images)
{
if (mergeHorizontal)
{
// Vertical alignment: top, center, bottom
int drawY = alignment == "top" ? 0 : alignment == "bottom" ? maxHeight - img.Height : (maxHeight - img.Height) / 2;
graphics.DrawImage(img, new Aspose.Imaging.Rectangle(x, drawY, img.Width, img.Height));
x += img.Width + padding;
}
else
{
// Horizontal alignment: left, center, right
int drawX = alignment == "left" ? 0 : alignment == "right" ? maxWidth - img.Width : (maxWidth - img.Width) / 2;
graphics.DrawImage(img, new Aspose.Imaging.Rectangle(drawX, y, img.Width, img.Height));
y += img.Height + padding;
}
}
outImg.Save("./output/merged_aligned.png");
}
images.ForEach(img => img.Dispose());
Step 4: Test, Preview, and Adjust
- Adjust
alignment
andpadding
for best look. - Change background color if needed for style or transparency.
Use Cases and Applications
- Scanned document page collages
- Portfolio layouts from mixed camera uploads
- E-commerce comparison shots (varied products)
- Social media composite images
Common Challenges and Solutions
Challenge 1: Large Padding or Gaps
Solution: Tune padding, background color, or resize input images before merging.
Challenge 2: Unintended Cropping or Overlap
Solution: Double-check canvas size and placement math.
Challenge 3: Alignment Looks Wrong in Output
Solution: Try different alignment options, preview in context.
Performance Considerations
- Dispose images after merging to avoid memory issues
- Use PNG for best quality and lossless output
- For many images, preview with a small sample first
Best Practices
- Decide alignment based on image content (e.g., center faces, align product bases)
- Document parameters for automation
- Preview result before full rollout
- Use clear, descriptive filenames
Advanced Scenarios
Scenario 1: Use Dynamic Padding or Color Per Image
Allow per-image customizations (brand color, thicker gaps for sections).
Scenario 2: Add Borders or Annotations
Overlay lines, text, or shapes for clarity and style.
FAQ
Q: Can I mix formats or color depths? A: Yes, Aspose.Imaging normalizes output to your export type.
Q: How do I add transparent padding?
A: Use PNG and set background to Color.Transparent
.
Q: Can I align based on content? A: Use object/face detection APIs to calculate dynamic alignment.
Conclusion
Merging images of any size, with perfect alignment and padding, is simple and professional with Aspose.Imaging for .NET—unlocking beautiful, consistent results for every project.
See Aspose.Imaging for .NET API Reference for more advanced alignment, padding, and merge options.