翻譯|使用教程|編輯:李顯亮|2021-08-11 09:52:31.763|閱讀 631 次
概述:在本文中,將學(xué)習(xí)如何使用 C# 以編程方式將多個圖像合并或組合成單個圖像。分步指南和代碼示例將演示如何水平或垂直合并圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
在本文中,將學(xué)習(xí)如何使用 C# 以編程方式將多個圖像合并或組合成單個圖像。分步指南和代碼示例將演示如何水平或垂直合并圖像。
為了將多個圖像合并為一個圖像,我們將使用Aspose.Imaging,它是一個強(qiáng)大且功能豐富的圖像處理 API,可讓您處理各種圖像格式。還沒使用過的朋友可以點(diǎn)擊下載最新版
 
 
有兩種方法可以將圖像合并為一個:垂直和水平。在前一種方法中,圖像在垂直方向上相互附加,而在后一種方法中,圖像在水平方向上一個接一個地組合。
以下是使用 C# 垂直合并圖像的步驟。
以下代碼示例展示了如何垂直合并圖像。
// Create a list of images
string[] imagePaths = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg", "image5.png" };
// Get resulting image's size
ListimageSizes = new List();
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);
// 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() { Source = outputStreamSource, Quality = 100 };
    
    // Create output image
    using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
    {
        int stitchedHeight = 0;
        // Merge images
        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;
            }
        }
        
        // Save the merged image
        newImage.Save("merged-image.jpg");
    }
}
以下是使用 C# 水平組合圖像的步驟。
以下代碼示例展示了如何水平合并多個圖像。
// Create list of images
string[] imagePaths = { "image1.jpg", "image2.jpg", "image3.jpg", "image4.JPG", "image5.png" };
// To create a temporary image
string tempFilePath = "temp.jpg";
// Get resulting image's size
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);
// Combine images into new one
Source tempFileSource = new FileCreateSource(tempFilePath, isTemporal: true);
// Create jpeg options
JpegOptions options = new JpegOptions() { Source = tempFileSource, Quality = 100 };
using (JpegImage newImage = (JpegImage)Image.Create(options, newWidth, newHeight))
{
    int stitchedWidth = 0;
    
    // Merge images
    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;
        }
    }
    
    // Save the merged image
    newImage.Save(outputPath);
}
如果你想試用Aspose的全部完整功能,可聯(lián)系在線客服獲取30天臨時授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@ke049m.cn