Select language
  1. Products
  2. Aspose.Imaging
  3. Image merge
clearbit icon

Merge images for .NET

Convenient tools to merge several images into one, creating image or photo collages. Merge is supported for wide range of image formats.

Share
Instagram Logo Dribbble Logo Twitter Logo Youtube Logo

How to Merge the Images File Using .NET Library

In order to merge Image files into one, we’ll use Aspose.Imaging for .NET API which is a feature-rich, powerful and easy to use image manipulation API for net platform. Open NuGet package manager, search for Aspose.Imaging and install. You may also use the following command from the Package Manager Console.

1
Install-Package Aspose.Imaging

Merge Images via .NET

You need Aspose.Imaging for .NET and Aspose.Imaging.Merge.NET license to try the code in your environment.

  1. Load input Images to collect the reference data.
  2. Create output image taking into consideration input reference data and merge layout (horizontal, vertical,, tile, etc)
  3. Create the Graphics class for output image and draw the input images using selected merge strategy.
  4. You may pre-resize input images to fit the same or required size.

System Requirements

Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Core
  • Development environment like Visual Studio Code or Microsoft Visual Studio.
  • Aspose.Imaging for .NET DLL referenced in your project.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using System;
 using Aspose.Imaging;
 using Aspose.Imaging.FileFormats.Png

namespace CSharpTutorials
{
    enum MergeDirection
    {
           Horizontal = 0,
           Vertical = 1
    }

    class Program
    {
        static void Main(string[] args)
        {
            if(args.Lenght<3)
            {
                Console.WriteLine("Please specify inpur folder with images to merge and ");
                Console.WriteLine("wildcard mask of the input images (i.e. *.png or *.jpg) and");
                Console.WriteLine("output folder for merge image(s)");
                return;
            }

            License license = new License();
            license.SetLicense("Aspose.Imaging.Merge.NET.lic");
            // This code merges input images into output one
            var images = new List<Image>();

            int maxWidth = 0;
            int maxHeight = 0;
            int totalWidth = 0;
            int totalHeight = 0;            

            string InputDirectory = args[0];
            string Mask = args[1];
                
            foreach (var fileName in Directory.GetFiles(InputDirectory, Mask))
            {
                    var image = Image.Load(fileName);
    
                    totalWidth += image.Width;
    
                    if (image.Width > maxWidth)
                    {
                        maxWidth = image.Width;
                    }
    
                    totalHeight += image.Height;
    
                    if (image.Height > maxHeight)
                    {
                        maxHeight = image.Height;
                    }
    
                    images.Add(image);
            }
    
            string OutputDirectory = args[2];
            if (!Directory.Exists(OutputDirectory))
            {
                Directory.CreateDirectory(OutputDirectory);
            }

            try
            {
                var outputPath = Path.Combine(OutputDirectory, "merge_horizontal.jpg");
                var outputOptions = new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha };
                MergeImages(images, MergeDirection.Horizontal, totalWidth, maxHeight, outputPath, outputOptions);
    
                outputPath = Path.Combine(OutputDirectory, "merge_vertical.jpg");
                MergeImages(images, MergeDirection.Vertical, totalHeight, maxWidth, outputPath, outputOptions);
            }
            finally
            {
                images.ForEach(image => image.Dispose());
            }
        }            

        void MergeImages(List<Image> images, MergeDirection direction, int totalSize, int maxSize, string outputPath, ImageOptionsBase outputImageOptions)
        {
            int targetWidth, targetHeight;
                
            switch (direction)
            {
                case MergeDirection.Horizontal:
                {
                    targetWidth = totalSize;
                    targetHeight = maxSize;
                    break;
                }
                case MergeDirection.Vertical:
                {
                    targetWidth = maxSize;
                    targetHeight = totalSize;
                    break;
                }
                default:
                    throw new ArgumentException("Unexpected merge direction");
            }
                
            using (Stream stream = new MemoryStream())
            {
                outputOptions.Source = new StreamSource(stream);
            
                using (var image = Image.Create(outputOptions, targetWidth, targetHeight))
                {
                    image.BackgroundColor = Color.White;
                    var graphics = new Graphics(image);
            
                    float x = 0, y = 0;
                    images.ForEach(image =>
                    {
                        graphics.DrawImage(image, new RectangleF(x, y, image.Width, image.Height));
            
                        if (direction == MergeDirection.Horizontal)
                        {
                            x += image.Width;
                        }
            
                        if (direction == MergeDirection.Vertical)
                        {
                            y += image.Height;
                        }
                    });
                                
                    image.Save(outputPath);
                }
            }
        }
    }
}

You may find other allowed image merge scenarious and examples here