حدد اللغة
  1. Products
  2. Aspose.Imaging
  3. Image merge
clearbit icon

دمج الصور ل.NET

أدوات ملائمة لدمج عدة صور في صورة واحدة، وإنشاء صور مجمعة أو صور مجمعة. مجموعة واسعة من تنسيقات الصور.

شراء ل $99
يشارك
شعار Instagram شعار Dribbble شعار Twitter شعار Youtube

كيفية دمج ملف الصور باستخدام مكتبة .NET

من أجل دمج ملفات الصور في ملف واحد، سنستخدم Aspose.Imaging for .NET API وهي واجهة برمجة تطبيقات غنية بالميزات وقوية وسهلة الاستخدام لمعالجة الصور للنظام الأساسي للشبكة.

1
Install-Package Aspose.Imaging

دمج الصور عبر .NET

انت تحتاج Aspose.Imaging Merge لترخيص .NET المقنن لتجربة الكود في بيئتك.

  1. تحميل صور الإدخال لجمع البيانات المرجعية.
  2. إنشاء صورة مخرجة مع الأخذ في الاعتبار البيانات المرجعية المدخلة وتخطيط الدمج (أفقي، رأسي، تجانبي، إلخ)
  3. قم بإنشاء فئة الرسومات لصورة الإخراج ورسم الصور المدخلة باستخدام استراتيجية الدمج المحددة.
  4. يمكنك تغيير حجم الصور المدخلة مسبقًا لتناسب الحجم نفسه أو الحجم المطلوب.

متطلبات النظام

فقط تأكد من أن لديك المتطلبات الأساسية التالية.

  • Microsoft Windows أو نظام تشغيل متوافق مع .NET Core
  • بيئة التطوير مثل Visual Studio Code أو Microsoft Visual Studio.
  • Aspose.Imaging لـ .NET DLL المشار إليه في مشروعك.
  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