複数のJPG画像を組み合わせて画像を作成する必要がある場合があります。この記事では、C#でこれを行う方法について説明します。

C#Imaging API –無料ダウンロード

Aspose.Imaging for .NETは、.NETアプリケーション内でイメージを作成および操作するための柔軟なルーチンを多数提供します。 JPGファイルを数行のコードで組み合わせることができます。 NuGetまたはdownloadAPIのDLLを使用して、.NETアプリケーション内にインストールできます。

Install-Package Aspose.Imaging

JPG画像をC#で水平方向にマージ

以下は、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);

// 画像を1つにマージします。
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画像を垂直方向にマージする手順は上記と同じです。少しの違いは、配列内のすべての画像の高さを加算して結果の画像の高さを計算し、配列内の画像の最大幅を見つけて幅を計算することです。次のコードサンプルは、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);

// 画像を1つにマージします。
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);
    }
}

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);

// 画像を1つにマージします。
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);

// 画像を1つにマージし、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.Imagingfor.NETのドキュメントを確認してください。ご不明な点がございましたら、サポートフォーラムまでお気軽にお問い合わせください。

関連項目