Merge Images in C#

Aspose.Imaging’s Image Merger Plugin for .NET simplifies the task of programmatically combining images seamlessly, either horizontally or vertically, without any quality loss. This plugin, tailored for developers, supports a wide range of formats and empowers users to craft stunning photo collages effortlessly. In this article, you’ll learn how to merge or combine multiple images programmatically in C#.


Topics Covered:


C# API to Merge Images

To combine images C# programmatically, we will use Aspose.Imaging for .NET, a robust .NET image processing library for merging. This high-performance .NET image merging API allows for precise image manipulations and supports merging images in various formats. You can easily integrate the API into your .NET projects by downloading the plugin or installing it via NuGet.

PM> Install-Package Aspose.Imaging

Info: Other APIs from Aspose, especially Aspose.Slides for .NET, may also allow you to merge images (JPG or PNG), PDFs, HTML docs, and files in other formats.

Merge Multiple Images in C#

There are two ways to merge images into a single one: vertical image concatenation and horizontal image concatenation. In vertical image concatenation, the images are appended to each other vertically, whereas, in horizontal concatenation, images are combined one after another horizontally. In the following sections, you will learn both of the methods with code samples.

C# Merge Images Vertically

The following are the steps to merge images vertically C#.

  • First, specify the paths of the images in a string array.
  • Then, create a List of Size and store the 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 in C#.

C# Merge Images Horizontally

The following are the steps to combine images horizontally C#.

  • First, specify the paths of the images in a string array.
  • Then, create a List of Size and store the 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 the 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 the 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 C#.

C# Image Merging API - Get a Free License

You can get a free temporary license and merge the images without evaluation limitations.

Conclusion

In this article, you have learned how to programmatically merge images into a single image using C#. The code samples have demonstrated how to combine images programmatically both vertically and horizontally. In addition, you can explore more about the .NET image processing library for merging using the documentation. Also, you can share your queries with us via our forum.

See Also

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.