How to Merge Images and Add Borders or Labels Using .NET
Adding borders and captions to merged images improves clarity and professionalism—essential for technical reports, galleries, and marketing materials. Aspose.Imaging for .NET makes this easy to automate for any batch or scenario.
Real-World Problem
Simply merging images can lead to visual confusion, especially in galleries or side-by-side comparisons. Borders separate images clearly, while labels provide context, dates, or product info.
Solution Overview
Aspose.Imaging’s Graphics API lets you draw borders, fill backgrounds, and overlay text on any image during merge—scalable for any batch, style, or automation need.
Prerequisites
- Visual Studio 2019 or later
- .NET 6.0 or later (or .NET Framework 4.6.2+)
- Aspose.Imaging for .NET from NuGet
- Images to merge and annotate (any format)
PM> Install-Package Aspose.Imaging
Step-by-Step Implementation
Step 1: Choose Merge Layout and Prepare Images
bool mergeHorizontal = true; // or false for vertical
string[] files = Directory.GetFiles("./input", "*.jpg");
Step 2: Load Images, Define Border/Label Settings
var images = files.Select(f => Image.Load(f)).ToList();
int borderThickness = 5;
Color borderColor = Color.Black;
Font labelFont = new Font("Arial", 24, FontStyle.Bold);
Color labelColor = Color.Blue;
string[] labels = files.Select(Path.GetFileNameWithoutExtension).ToArray();
Step 3: Calculate Canvas Size with Space for Borders and Labels
int labelHeight = 40;
int totalWidth = mergeHorizontal ? images.Sum(i => i.Width + borderThickness * 2) : images.Max(i => i.Width) + borderThickness * 2;
int totalHeight = mergeHorizontal ? images.Max(i => i.Height) + borderThickness * 2 + labelHeight : images.Sum(i => i.Height + borderThickness * 2 + labelHeight);
Step 4: Draw Each Image with Border and Caption
using (var outImg = Image.Create(new PngOptions(), totalWidth, totalHeight))
{
var graphics = new Aspose.Imaging.Graphics(outImg);
graphics.Clear(Color.White);
int x = 0, y = 0;
for (int i = 0; i < images.Count; i++)
{
var img = images[i];
int drawX = mergeHorizontal ? x : (totalWidth - img.Width) / 2;
int drawY = mergeHorizontal ? (totalHeight - img.Height - labelHeight) / 2 : y;
// Draw border
graphics.DrawRectangle(new Pen(borderColor, borderThickness), drawX - borderThickness, drawY - borderThickness, img.Width + borderThickness * 2, img.Height + borderThickness * 2);
// Draw image
graphics.DrawImage(img, drawX, drawY, img.Width, img.Height);
// Draw label
graphics.DrawString(labels[i], labelFont, new SolidBrush(labelColor), drawX, drawY + img.Height + 5);
if (mergeHorizontal)
x += img.Width + borderThickness * 2;
else
y += img.Height + borderThickness * 2 + labelHeight;
}
outImg.Save("./output/merged_with_borders_labels.png");
}
images.ForEach(img => img.Dispose());
Step 5: Preview and Fine-Tune
- Test different fonts, colors, or border thicknesses
- Adjust position for long labels or small images
Use Cases and Applications
- Marketing and social media collages with product names
- Technical documentation and reports with figure captions
- Annotated photo walls for events or exhibitions
- E-commerce product comparison charts
Common Challenges and Solutions
Challenge 1: Labels Overlap Images or Borders
Solution: Increase labelHeight or adjust text position as needed.
Challenge 2: Borders Cover Image Content
Solution: Draw borders outside the image area (as in sample code).
Challenge 3: Font or Color Issues
Solution: Choose web-safe fonts and high-contrast label colors for readability.
Performance Considerations
- Dispose all images after drawing to avoid memory leaks
- Use PNG for clear borders and lossless quality; JPEG for web
- Test batch size and canvas size for large collages
Best Practices
- Keep border and label styles consistent for branding
- Preview output in the target app or platform
- Automate for dynamic folders or product lists
- Store or export source settings for repeat runs
Advanced Scenarios
Scenario 1: Custom Borders Per Image
Draw different colors/thickness for each photo or category.
Scenario 2: Rotated or Diagonal Labels
Use Graphics transformations for creative styles.
FAQ
Q: Can I use transparent or rounded borders? A: Yes, draw with semi-transparent colors or round rectangles for effect.
Q: How do I localize/caption in multiple languages?
A: Use a translation API to generate labels[]
before drawing.
Q: Can I automate label content? A: Yes, pull from file metadata, EXIF, or a database for dynamic labeling.
Conclusion
With Aspose.Imaging for .NET, it’s easy to create annotated, visually distinct merged images—automated for any workflow, from galleries to technical docs.
See Aspose.Imaging for .NET API Reference for more customization with borders, labels, and drawing.