Sprache auswählen
  1. Products
  2. Aspose.Imaging
  3. Image merge
clearbit icon

Bilder für .NET zusammenführen

Praktische Tools zum Zusammenführen mehrerer Bilder zu einem und zum Erstellen von Bild- oder Fotocollagen. große Auswahl an Bildformaten.

So führen Sie die Bilddatei mithilfe der .NET-Bibliothek zusammen

Um Bilddateien zu einer zusammenzuführen, verwenden wir die Aspose.Imaging for .NET API, eine funktionsreiche, leistungsstarke und benutzerfreundliche Bildbearbeitungs-API für die Net-Plattform.

1
Install-Package Aspose.Imaging

Bilder über .NET zusammenführen

Du brauchst Aspose.Imaging Merge für .NET-Zähllizenz um den Code in Ihrer Umgebung auszuprobieren.

  1. Laden Sie Eingabebilder, um die Referenzdaten zu sammeln.
  2. Erstellen Sie ein Ausgabebild unter Berücksichtigung der Eingabereferenzdaten und des Zusammenführungslayouts (horizontal, vertikal, Kachel usw.).
  3. Erstellen Sie die Graphics-Klasse für das Ausgabebild und zeichnen Sie die Eingabebilder mit der ausgewählten Zusammenführungsstrategie.
  4. Sie können die Größe der Eingabebilder vorab ändern, um sie an die gleiche oder die erforderliche Größe anzupassen.

System Anforderungen

Stellen Sie einfach sicher, dass Sie die folgenden Voraussetzungen erfüllen.

  • Microsoft Windows oder ein kompatibles Betriebssystem mit .NET Core
  • Entwicklungsumgebung wie Visual Studio Code oder Microsoft Visual Studio.
  • Aspose.Imaging für .NET DLL, auf das in Ihrem Projekt verwiesen wird.
  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
133
134
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;
            }

            Metered metered = new Metered();
            metered.SetLicense("***********", // public key
                               "***********"  // private key
                              );
            // 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