在 C# 中合併圖像

在本文中,您將學習如何使用 C# 以編程方式將多個圖像合併或合併為單個圖像。分步指南和代碼示例將演示如何水平或垂直合併圖像。

用於合併圖像的 C# API - 免費下載

為了將多個圖像合併為一個圖像,我們將使用 Aspose.Imaging for .NET。它是一個功能強大且功能豐富的圖像處理 API,可讓您處理各種圖像格式。您可以 下載 API 或使用 NuGet 安裝它。

PM> Install-Package Aspose.Imaging

在 C# 中合併多個圖像

有兩種方法可以將圖像合併為一個:垂直和水平。在前一種方法中,圖像在垂直方向上相互附加,而在後一種方法中,圖像在水平方向上一個接一個地組合。在以下部分中,您將通過代碼示例了解這兩種方法。

C# 垂直合併圖像

以下是使用 C# 垂直合併圖像的步驟。

  • 首先,在字符串數組中指定圖像的路徑。
  • 然後,創建一個 Size 列表並將每個圖像的大小存儲到其中。
  • 計算結果圖像的高度和寬度。
  • 創建一個 StreamSource 類的對象並用一個新的 MemoryStream 初始化它。
  • 創建一個 JpegOptions 類的對象並設置其選項。
  • 為新圖像實例化 JpegImage 類,並使用 JpegOptions 初始化它併計算高度和寬度。
  • 遍歷圖像列表,並在每次迭代中將圖像加載到 RasterImage 對像中。
  • 為每個圖像創建一個 Rectangle,並使用 JpegImage.SaveArgb32Pixels() 方法將其添加到新圖像中。
  • 在每次迭代中增加縫合高度。
  • 最後,使用 JpegImage.Save(string) 方法保存新圖像。

以下代碼示例顯示瞭如何在 C# 中垂直合併圖像。

// 創建圖像列表
string[] imagePaths = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.png" };

// 獲取結果圖像的大小
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);
    
    // 創建 jpeg 選項
    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("merged-image.jpg");
    }
}

在 C# 中水平合併圖像

下面是使用C#水平合併圖片的步驟。

  • 首先,在字符串數組中指定圖像的路徑。
  • 然後,創建一個 Size 列表並將每個圖像的大小存儲到其中。
  • 計算結果圖像的高度和寬度。
  • 使用 FileCreateSource(String, Boolean) 創建一個新源,並使用文件路徑對其進行初始化。
  • 創建一個 JpegOptions 類的對象並設置其選項。
  • 為新圖像實例化 JpegImage 類,並使用 JpegOptions 初始化它併計算高度和寬度。
  • 遍歷圖像列表,並在每次迭代中將圖像加載到 RasterImage 對像中。
  • 為每個圖像創建一個 Rectangle 並使用 JpegImage.SaveArgb32Pixels() 方法將其添加到新圖像。
  • 在每次迭代中增加縫合寬度。
  • 完成後,使用 JpegImage.Save(string) 方法保存新圖像。

以下代碼示例顯示瞭如何水平合併多個圖像。

// 創建圖像列表
string[] imagePaths = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.JPG", "image5.png" };

// 創建臨時圖像
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);

// 創建 jpeg 選項
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);
}

C# 圖像合併 API - 獲取免費許可證

您可以 獲得免費的臨時許可證 並在沒有評估限制的情況下合併圖像。

結論

在本文中,您學習瞭如何使用 C# 將多個圖像合併為一個圖像。代碼示例演示瞭如何垂直和水平組合圖像。此外,您可以使用 文檔 探索有關 .NET 圖像處理 API 的更多信息。此外,您可以通過我們的 論壇 與我們分享您的疑問。

也可以看看

信息:Aspose 提供了一個免費的拼貼網絡應用程序。使用此在線服務,您可以合併 JPG 到 JPG 或 PNG 到 PNG 圖像,創建照片網格,等等。