บางครั้งเราจำเป็นต้องสร้างภาพโดยการรวมภาพ JPG หลายภาพเข้าด้วยกัน บทความนี้จะอธิบายวิธีการทำเช่นนี้ใน C#

C# Imaging API – ดาวน์โหลดฟรี

Aspose.Imaging for .NET มีกิจวัตรที่ยืดหยุ่นหลายประการสำหรับการสร้างและจัดการรูปภาพภายในแอปพลิเคชัน .NET ช่วยให้คุณรวมไฟล์ JPG เข้ากับโค้ดไม่กี่บรรทัด คุณสามารถติดตั้งภายในแอปพลิเคชัน .NET ของคุณโดยใช้ NuGet หรือ ดาวน์โหลด DLL ของ API

Install-Package Aspose.Imaging

รวมรูปภาพ JPG ในแนวนอนใน C#

ต่อไปนี้เป็นขั้นตอนในการรวมภาพ JPEG ในแนวนอนใน C#

  • สร้างอาร์เรย์ของภาพ JPEG ที่คุณต้องการรวม
  • คำนวณความกว้างของรูปภาพผลลัพธ์โดยเพิ่มความกว้างของรูปภาพทั้งหมดในอาร์เรย์และความสูงโดยค้นหาความสูงสูงสุดของรูปภาพในอาร์เรย์
  • สร้างภาพใหม่โดยใช้คลาส JpegImage และตั้งค่าความกว้างและความสูงเป็นภาพที่คำนวณในขั้นตอนสุดท้าย
  • วนซ้ำอาร์เรย์ของรูปภาพ (ที่คุณต้องการผสาน) และดำเนินการต่อไปนี้กับแต่ละรูปภาพ:
    • โหลดพิกเซลของรูปภาพโดยใช้วิธี LoadArgb32Pixels และบันทึกลงในภาพที่ได้โดยใช้วิธี SaveArgb32Pixels วิธีนี้ยังใช้วัตถุ Rectangle เป็นอาร์กิวเมนต์ที่กำหนดตำแหน่งของรูปภาพในรูปภาพผลลัพธ์
  • บันทึกภาพที่ได้เป็นภาพ 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);
}

ป้อนรูปภาพ

รูปภาพเอาต์พุต

รวมรูปภาพ 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);
    }
}

รวมรูปภาพ 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 หากคุณมีคำถามใด ๆ โปรดติดต่อเราที่ ฟอรัมการสนับสนุนของเรา

ดูสิ่งนี้ด้วย