نحتاج أحيانًا إلى إنشاء صورة من خلال الجمع بين عدة صور بتنسيق JPG معًا. تشرح هذه المقالة كيفية القيام بذلك في C #.

C # Imaging API - تنزيل مجاني

يوفر Aspose.Imaging for .NET عددًا من الإجراءات المرنة لإنشاء الصور ومعالجتها داخل تطبيقات .NET. يتيح لك دمج ملفات JPG في بضعة أسطر من التعليمات البرمجية. يمكنك إما تثبيته داخل تطبيق .NET باستخدام NuGet أو تنزيل DLL الخاصة بواجهة برمجة التطبيقات.

Install-Package Aspose.Imaging

دمج صور JPG أفقيًا في C

فيما يلي خطوات دمج صور JPEG أفقيًا في C #.

  • قم بإنشاء مجموعة من صور JPEG التي تريد دمجها.
  • احسب عرض الصورة الناتجة عن طريق إضافة عرض كل الصور في المصفوفة والارتفاع عن طريق إيجاد أقصى ارتفاع للصورة في المصفوفة.
  • أنشئ صورة جديدة باستخدام فئة JpegImage واضبط عرضها وارتفاعها على الصورة المحسوبة في الخطوة الأخيرة.
  • كرر مجموعة الصور (التي تريد دمجها) وقم بتنفيذ المهام التالية على كل صورة:
    • قم بتحميل وحدات بكسل من الصورة باستخدام طريقة LoadArgb32Pixels واحفظها في الصورة الناتجة باستخدام طريقة SaveArgb32Pixels. تأخذ هذه الطريقة أيضًا كائن مستطيل كوسيطة تحدد موضع الصورة في الصورة الناتجة.
  • احفظ الصورة الناتجة كصورة JPEG.

يوضح نموذج التعليمات البرمجية التالي كيفية الانضمام إلى صور JPEG أفقيًا في C #.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats.Jpeg;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;

// مجموعة من الصور التي تريد دمجها.
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" };

// اسم الملف الناتج / الناتج.
string outputPath = "MergeImages_Horizontally.jpg";

// الحصول على حجم الصورة الناتج.
List<Size> imageSizes = new List<Size>();
foreach (string imagePath in imagePaths)
{
    using (RasterImage image = (RasterImage)Image.Load(imagePath))
    {
        imageSizes.Add(image.Size);
    }
}

// عرض وارتفاع الصورة الناتجة
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);

// دمج الصور في صورة واحدة.
Source tempFileSource = new FileCreateSource("temp.jpg", isTemporal: true);
JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 };
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
{
    int stitchedWidth = 0;
    foreach (string imagePath in imagePaths)
    {
        using (RasterImage image = (RasterImage)Image.Load(imagePath))
        {
            Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height);
            newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
            stitchedWidth += image.Width;
        }
    }

    newImage.Save(outputPath);
}

إدخال الصور

First Image
Second Image

صورة الإخراج

Horizontally Merged Image

دمج صور JPG عموديًا في C

خطوات دمج صور JPEG عموديًا هي نفسها المذكورة أعلاه. هناك اختلاف بسيط وهو أننا نحسب ارتفاع الصورة الناتجة عن طريق إضافة ارتفاع جميع الصور في المصفوفة والعرض من خلال إيجاد أقصى عرض للصورة في المصفوفة. يوضح نموذج التعليمات البرمجية التالي كيفية الانضمام إلى صور JPEG عموديًا في C #.

// مجموعة من الصور التي تريد دمجها
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" };

string outputPath = "MergeImages_Vertically.jpg";

// الحصول على حجم الصورة الناتج.
List<Size> imageSizes = new List<Size>();
foreach (string imagePath in imagePaths)
{
    using (RasterImage image = (RasterImage)Image.Load(imagePath))
    {
        imageSizes.Add(image.Size);
    }
}

// عرض وارتفاع الصورة الناتجة
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);

// دمج الصور في صورة واحدة.
using (MemoryStream memoryStream = new MemoryStream())
{
    StreamSource outputStreamSource = new StreamSource(memoryStream);
    JpegOptions options = new JpegOptions() { Source = outputStreamSource, Quality = 100 };
    using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
    {
        int stitchedHeight = 0;
        foreach (string imagePath in imagePaths)
        {
            using (RasterImage image = (RasterImage)Image.Load(imagePath))
            {
                Rectangle bounds = new Rectangle(0, stitchedHeight, image.Width, image.Height);
                newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
                stitchedHeight += image.Height;
            }
        }

        newImage.Save(outputPath);
    }
}
Vertically Merged Image

دمج صور JPG في PDF في C

قد تحتاج إلى دمج صور JPEG في ملف PDF. يمكنك القيام بذلك عن طريق إجراء تغيير طفيف في طريقة Image.Save (استخدم امتداد .pdf بدلاً من .jpg).

// مجموعة من الصور التي تريد دمجها.
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" };

string outputPath = "MergeHorizontalAsPDF";
string tempFilePath = "temp.jpg";

// الحصول على حجم الصورة الناتج.
List<Size> imageSizes = new List<Size>();
foreach (string imagePath in imagePaths)
{
    using (RasterImage image = (RasterImage)Image.Load(imagePath))
    {
        imageSizes.Add(image.Size);
    }
}

// عرض وارتفاع الصورة الناتجة.
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);

// دمج الصور في صورة واحدة.
Source tempFileSource = new FileCreateSource(tempFilePath, isTemporal: true);
JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 };
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
{
    int stitchedWidth = 0;
    foreach (string imagePath in imagePaths)
    {
        using (RasterImage image = (RasterImage)Image.Load(imagePath))
        {
            Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height);
            newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
            stitchedWidth += image.Width;
        }
    }

    newImage.Save(outputPath + ".pdf", new PdfOptions());
}

دمج صور JPG في PNG في C

وبالمثل ، قد ترغب في دمج صور JPEG في PNG. كما هو موضح أعلاه ، تحتاج فقط إلى استخدام امتداد .png بدلاً من .jpg في طريقة Image.Save.

// مجموعة من الصور التي تريد دمجها.
string[] imagePaths = { "Image1.jpeg", "Image2.jpg" };

string outputPath = "MergeHorizontalAsPNG";

// الحصول على حجم الصورة الناتج.
List<Size> imageSizes = new List<Size>();
foreach (string imagePath in imagePaths)
{
    using (RasterImage image = (RasterImage)Image.Load(imagePath))
    {
        imageSizes.Add(image.Size);
    }
}

// عرض وارتفاع الصورة الناتجة.
int newWidth = imageSizes.Sum(size => size.Width);
int newHeight = imageSizes.Max(size => size.Height);

// دمج الصور في صورة واحدة وحفظها بتنسيق PNG
using (MemoryStream memoryStream = new MemoryStream())
{
    StreamSource outputStreamSource = new StreamSource(memoryStream);
    JpegOptions options = new JpegOptions() { Source = outputStreamSource, Quality = 100 };
    using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
    {
        int stitchedWidth = 0;
        foreach (string imagePath in imagePaths)
        {
            using (RasterImage image = (RasterImage)Image.Load(imagePath))
            {
                Rectangle bounds = new Rectangle(stitchedWidth, 0, image.Width, image.Height);
                newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
                stitchedWidth += image.Width;
            }
        }

        newImage.Save(outputPath + ".png", new PngOptions());
    }
}

استنتاج

في هذه المقالة ، تعلمت كيفية الانضمام إلى صور JPEG في C #. يمكنك إما دمجها أفقيًا أو رأسيًا. لقد تعلمت أيضًا كيفية حفظ الصورة المدمجة كملف PDF أو PNG. لمزيد من المعلومات ، يرجى مراجعة التوثيق الخاص بـ Aspose.Imaging for .NET. إذا كان لديك أي سؤال ، فلا تتردد في الاتصال بنا على منتدى الدعم.

أنظر أيضا