C# で写真を結合する方法

このトピックでは、C# で photos をマージする方法について説明します。これには、C# でフォト ジョイナーを開発するための環境構成、段階的なプロセス、および実行可能なコード スニペットが含まれています。この情報を適用して、さまざまなオペレーティング システムの .NET がサポートされている環境でこの機能を使用できます。

C# で写真を結合する手順

  1. NuGet パッケージ マネージャー プラグインを使用して、Aspose.Imaging for .NET と連携する環境をセットアップします
  2. 画像のリストを作成し、結果の画像のサイズを取得します
  3. 画像を結合して新しい画像を作成し、出力ソースを作成します
  4. JpegOptions クラス オブジェクトを使用してさまざまなプロパティを設定します
  5. JpegImage クラスを使用して、結合された画像をエクスポートします

これらの手順は、C# で写真を結合するプロセス全体を要約したものです。まず、1 枚の写真に結合する複数の画像のリストを作成します。したがって、必要に応じてディスクまたはストリームに保存する前に、新しいピクチャを作成して出力イメージにレンダリングします。

C# で写真を結合するコード

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Aspose.Imaging;
using Aspose.Imaging.ImageOptions;
using Aspose.Imaging.Sources;
class Program
{
static void Main(string[] args) // Merge images into one image in C#
{
// Set PDF license
new Aspose.Imaging.License().SetLicense("License.lic");
// Create a list of images
string[] imagePaths = { "picture1.jpg", "picture2.jpg", "picture3.jpg" };
// Get resulting image's size
List<Aspose.Imaging.Size> imageSizes = new List<Aspose.Imaging.Size>();
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Aspose.Imaging.Image.Load(imagePath))
{
imageSizes.Add(image.Size);
}
}
int newWidth = imageSizes.Max(size => size.Width);
int newHeight = imageSizes.Sum(size => size.Height);
// Combine images into new one
using (MemoryStream memoryStream = new MemoryStream())
{
// Create output source
StreamSource outputStreamSource = new StreamSource(memoryStream);
// Create jpeg options
JpegOptions options = new JpegOptions()
{ Quality = 100, Source = outputStreamSource };
// Create output image
using (Aspose.Imaging.FileFormats.Jpeg.JpegImage newImage =
(Aspose.Imaging.FileFormats.Jpeg.JpegImage)Aspose.Imaging.Image.Create(options, newWidth, newHeight))
{
int stitchedHeight = 0;
// Merge images
foreach (string imagePath in imagePaths)
{
using (RasterImage image = (RasterImage)Aspose.Imaging.Image.Load(imagePath))
{
Aspose.Imaging.Rectangle bounds = new Aspose.Imaging.Rectangle(0, stitchedHeight, image.Width, image.Height);
newImage.SaveArgb32Pixels(bounds, image.LoadArgb32Pixels(image.Bounds));
stitchedHeight += image.Height;
}
}
// Save the merged image
newImage.Save("merged-image.jpg");
}
}
System.Console.WriteLine("Done");
}
}

このコード スニペットは、C#* で *画像コンバイナーを開発するのに十分です。 JpegImage クラスと連携して、ソース画像の画像サイズを計算しながら出力画像を作成します。次に、四角形の作成に進み、Save メソッドを使用して結合された出力イメージを保存します。さらに、JpegOptions クラスを使用して、要件に応じて圧縮タイプ、品質、色のタイプ、解像度単位などの出力画像のいくつかのプロパティをカスタマイズできます。

この記事では、C#* で *イメージのマージを実行するプロセスを学習しました。画像を回転したい場合は、C#で画像を回転する方法 の記事をお読みください。

 日本語