In this article, you will learn how to merge or combine multiple images into a single image programmatically using C#. The step-by-step guide and the code samples will demonstrate how to merge images horizontally or vertically.
INFO: Aspose provides a FREE Collage web app. Using this online service, you can merge JPG to JPG or PNG to PNG images, create photo grids, and so on.
C# API to Merge Images
In order to merge multiple images into a single image, we will use Aspose.Imaging for .NET. It is a powerful and feature-rich image processing API that lets you manipulate a wide range of image formats. You can either download the API or install it using NuGet.
PM> Install-Package Aspose.Imaging
Merge Multiple Images using C#
There are two ways to merge images into a single one: vertically and horizontally. In the former method, the images are appended to each other vertically, whereas, in the latter, images are combined one after another horizontally. In the following sections, you will learn both of the methods with code samples.
Merge Images Vertically
The following are the steps to merge images vertically using C#.
- First, specify the paths of the images in a string array.
- Then, create a List of Size and store size of each image into it.
- Calculate the height and width of the resultant image.
- Create an object of StreamSource class and initialize it with a new MemoryStream.
- Create an object of JpegOptions class and set its options.
- Instantiate JpegImage class for the new image and initialize it with JpegOptions and calculated height and width.
- Iterate through the list of images and in each iteration, load the image in a RasterImage object.
- Create a Rectangle for each image and add it to the new image using JpegImage.SaveArgb32Pixels() method.
- Increase the stitched height in each iteration.
- Finally, save the new image using JpegImage.Save(string) method.
The following code sample shows how to merge images vertically.
Merge Images Horizontally
The following are the steps to combine images horizontally using C#.
- First, specify the paths of the images in a string array.
- Then, create a List of Size and store size of each image into it.
- Calculate the height and width of the resultant image.
- Create a new source using FileCreateSource(String, Boolean) and initialize it with file’s path.
- Create an object of JpegOptions class and set its options.
- Instantiate JpegImage class for the new image and initialize it with JpegOptions and calculated height and width.
- Iterate through the list of images and in each iteration, load the image in a RasterImage object.
- Create a Rectangle for each image and add it to new image using JpegImage.SaveArgb32Pixels() method.
- Increase the stitched width in each iteration.
- Once done, save the new image using JpegImage.Save(string) method.
The following code sample shows how to merge multiple images horizontally.
Get a Free API License
You can get a free temporary license in order to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to merge multiple images into a single image using C#. The code samples have demonstrated how to combine the images vertically and horizontally. You can explore more about the .NET image processing API using documentation. Also, you can share your queries with us via our forum.