Merge Images in C#

Aspose.Imaging’s Image Merger Plugin for .NET streamlines the process of programmatically combining images either horizontally or vertically, ensuring no loss of quality. This plugin, designed specifically for developers, supports various formats and enables users to create stunning photo collages effortlessly. In this article, you will learn how to merge or combine multiple images programmatically in C#.


Topics Covered:


C# API to Merge Images

To combine images in C# programmatically, we will utilize Aspose.Imaging for .NET, a powerful .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

Merge Multiple Images in C#

There are two primary methods to merge images into a single file: vertical image concatenation and horizontal image concatenation. In vertical concatenation, images are appended one above the other, while in horizontal concatenation, images are placed side by side. The following sections will guide you through both methods with code samples.

C# Merge Images Vertically

To merge images vertically in C#, follow these steps:

  • First, specify the paths of the images in a string array.
  • Next, create a List of Size and store the size of each image.
  • 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 for the new image and initialize it with the specified JpegOptions and calculated dimensions.
  • Iterate through the list of images, loading each into a RasterImage object.
  • Create a Rectangle for each image and add it to the new image using JpegImage.SaveArgb32Pixels() method.
  • Increment the stitched height during each iteration.
  • Finally, save the new image using the JpegImage.Save(string) method.

The following code sample illustrates how to merge images vertically in C#.

C# Merge Images Horizontally

To combine images horizontally in C#, follow these steps:

  • First, specify the paths of the images in a string array.
  • Next, create a List of Size and store the size of each image.
  • 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 for the new image and initialize it with the specified JpegOptions and calculated dimensions.
  • Iterate through the list of images, loading each into a RasterImage object.
  • Create a Rectangle for each image and add it to the new image using JpegImage.SaveArgb32Pixels() method.
  • Increment the stitched width during each iteration.
  • Once completed, save the new image using the JpegImage.Save(string) method.

The following code sample demonstrates how to merge multiple images horizontally in C#.

C# Image Merging API - Get a Free License

You can obtain a free temporary license and merge images without evaluation restrictions.

Conclusion

In this article, you have learned how to programmatically merge images into a single file using C#. The code samples provided demonstrate how to combine images programmatically both vertically and horizontally. For further exploration of the .NET image processing library for merging, refer to the documentation. Additionally, feel free to share your queries with us via our forum.

See Also