때로는 여러 JPG 이미지를 결합하여 이미지를 만들어야 할 때가 있습니다. 이 문서에서는 C#에서 이 작업을 수행하는 방법을 설명합니다.

C# 이미징 API – 무료 다운로드

Aspose.Imaging for .NET은 .NET 응용 프로그램 내에서 이미지를 만들고 조작하기 위한 여러 가지 유연한 루틴을 제공합니다. 몇 줄의 코드로 JPG 파일을 결합할 수 있습니다. NuGet 또는 다운로드 API의 DLL을 사용하여 .NET 애플리케이션 내에 설치할 수 있습니다.

Install-Package Aspose.Imaging

C#에서 JPG 이미지를 가로로 병합

다음은 C#에서 JPEG 이미지를 수평으로 병합하는 단계입니다.

  • 병합하려는 JPEG 이미지 배열을 만듭니다.
  • 배열에 있는 모든 이미지의 너비와 배열에 있는 이미지의 최대 높이를 찾아 높이를 더하여 결과 이미지의 너비를 계산합니다.
  • JpegImage 클래스를 사용하여 새 이미지를 만들고 너비와 높이를 마지막 단계에서 계산한 것으로 설정합니다.
  • 병합하려는 이미지 배열을 반복하고 각 이미지에 대해 다음 작업을 수행합니다.
    • LoadArgb32Pixels 메서드를 사용하여 이미지의 픽셀을 로드하고 SaveArgb32Pixels 메서드를 사용하여 결과 이미지에 저장합니다. 이 메서드는 또한 Rectangle 객체를 결과 이미지에서 이미지의 위치를 정의하는 인수로 사용합니다.
  • 결과 이미지를 JPEG 이미지로 저장합니다.

다음 코드 샘플은 C#에서 JPEG 이미지를 가로로 결합하는 방법을 보여줍니다.

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

C#에서 JPG 이미지를 세로로 병합

JPEG 이미지를 세로로 병합하는 단계는 위와 동일합니다. 약간의 차이점은 배열에 있는 모든 이미지의 높이와 배열에 있는 이미지의 최대 너비를 찾아 너비를 더하여 결과 이미지의 높이를 계산한다는 것입니다. 다음 코드 샘플은 C#에서 JPEG 이미지를 세로로 결합하는 방법을 보여줍니다.

// 병합하려는 이미지 배열
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

C#에서 JPG 이미지를 PDF로 병합

JPEG 이미지를 PDF로 결합해야 할 수도 있습니다. Image.Save 메서드를 약간만 변경하면 됩니다(.jpg 대신 .pdf 확장명 사용).

// 병합하려는 이미지의 배열입니다.
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());
}

C#에서 JPG 이미지를 PNG로 병합

마찬가지로 JPEG 이미지를 PNG로 결합할 수 있습니다. 위와 같이 Image.Save 메서드에서 .jpg 대신 .png 확장자를 사용하면 됩니다.

// 병합하려는 이미지의 배열입니다.
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());
    }
}

결론

이 문서에서는 C#에서 JPEG 이미지를 결합하는 방법을 배웠습니다. 가로 또는 세로로 결합할 수 있습니다. 병합된 이미지를 PDF 또는 PNG로 저장하는 방법도 배웠습니다. 자세한 내용은 Aspose.Imaging for .NET의 문서를 참조하십시오. 질문이 있는 경우 지원 포럼으로 문의해 주십시오.

또한보십시오