How to Merge Photos in C#

This topic covers how to merge photos in C#. It encompasses the environment configuration, the step-by-step process, and a runnable code snippet to develop a photo joiner in C#. You can apply this information to work with this feature in any of the .NET-supported environments in different operating systems.

Steps to Merge Photos in C#

  1. Set up the environment to work with Aspose.Imaging for .NET using the NuGet package manager plugin
  2. Create a list of images and get the resulting image’s size
  3. Combine images into a new one and create an output source
  4. Set different properties using the JpegOptions class object
  5. Export the merged image using the JpegImage class

These steps summarize the whole process to combine photos in C#. First of all, create a list of multiple images to be merged into one photo. Consequently, create a new picture and render it to the output image before saving it to the disk or a stream depending on your needs.

Code to Merge Photos in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
class Program
{
static void Main(string[] args) // Merge images into one image in C#
{
// Set PDF license
new Aspose.Imaging.License().SetLicense("License.lic");
// Create a list of images
string[] imagePaths = { "picture1.jpg", "picture2.jpg", "picture3.jpg" };
// Get resulting image's size
List<Aspose.Imaging.Size> imageSizes = new List<Aspose.Imaging.Size>();
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Aspose.Imaging.Image.Load(imagePath))
{
imageSizes.Add(image.Size);
}
}
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);
// Combine images into new one
using (MemoryStream memoryStream = new MemoryStream())
{
// Create output source
StreamSource outputStreamSource = new StreamSource(memoryStream);
// Create jpeg options
JpegOptions options = new JpegOptions()
{ Quality = 100, Source = outputStreamSource };
// Create output image
using (Aspose.Imaging.FileFormats.Jpeg.JpegImage newImage =
(Aspose.Imaging.FileFormats.Jpeg.JpegImage)Aspose.Imaging.Image.Create(options, newWidth, newHeight))
{
int stitchedHeight = 0;
// Merge images
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Aspose.Imaging.Image.Load(imagePath))
{
Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(0, stitchedHeight, image.Width, image.Height);
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
stitchedHeight += image.Height;
}
}
// Save the merged image
newImage.Save("merged-image.jpg");
}
}
System.Console.WriteLine("Done");
}
}

This code snippet is sufficient to develop an image combiner in C#. It works with the JpegImage class to create the output image while calculating the image dimensions of the source pictures. Then it proceeds to create a rectangle and save the merged output image using the Save method. Furthermore, you can customize several properties of the output image with the JpegOptions class like the compression type, quality, color type, resolution unit, etc. as per your requirements.

In this article, we have learned the process of performing image merge in C#. If you want to rotate an image then read the article on how to rotate an image in C#.

 English