How to merge JPG to JPG programmatically

Compose horizontal layout

Aspose.Imaging allows to process JPG to JPG programmatically using Aspose.Imaging API. Below example shows how to perform JPG to JPG horizontal merge

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.jpg";
string tempFilePath = dataDir + "temp.jpg";
// Getting resulting image size.
List<Size> imageSizes = new List<Size>();
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Image.Load(imagePath))
{
imageSizes.Add(image.Size);
}
}
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);
// Combining images into new one.
Source tempFileSource = new FileCreateSource(tempFilePath, isTemporal: true);
JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 };
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
{
int stitchedWidth = 0;
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Image.Load(imagePath))
{
Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height);
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
stitchedWidth += image.Width;
}
}
newImage.Save(outputPath);
}
File.Delete(outputPath);
File.Delete(tempFilePath);

Compose vertical layout

Below example shows how to perform JPG to JPG vertical merge

using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.FileFormats.Tiff;
using Aspose.Imaging.FileFormats.Tiff.Enums;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
using Aspose.Imaging.Xmp;
using Aspose.Imaging.Xmp.Schemas.DublinCore;
using Aspose.Imaging.Xmp.Schemas.Photoshop;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
string templatesFolder = @"c:\Users\USER\Downloads\templates\";
string dataDir = templatesFolder;
string[] imagePaths = { dataDir + "template.jpg", dataDir + "template.jpeg" };
string outputPath = dataDir + "result.jpg";
string tempFilePath = dataDir + "temp.jpg";
// Getting resulting image size.
List<Size> imageSizes = new List<Size>();
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Image.Load(imagePath))
{
imageSizes.Add(image.Size);
}
}
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);
// Combining images into new one.
using (MemoryStream memoryStream = new MemoryStream())
{
StreamSource outputStreamSource = new StreamSource(memoryStream);
JpegOptions options = new JpegOptions() { Source = outputStreamSource, Quality = 100 };
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
{
int stitchedHeight = 0;
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Image.Load(imagePath))
{
Rectangle bounds = new Rectangle(0, stitchedHeight, image.Width, image.Height);
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
stitchedHeight += image.Height;
}
}
newImage.Save(outputPath);
}
}
File.Delete(outputPath);