マニュアルでモザイクの配置を作成することは、特に数十または数百の画像を処理するとき、悲惨でエラーに直面するプロセスです。この課題は、複数のプロジェクトやキャンペーンを通じて一貫性を維持する必要がある場合、さらに挑戦的になります.

このブログの投稿では、Aspose.Imaging for .NET を使用して複数の画像をネットワークレイアウトに統合するプロセスを自動化する方法を調べます.我々はあなたの環境を設定し、ソースイメージを編集して、それらをグリッドフォーマットでリサイクルおよび整理することからすべてをカバーします.このガイドの終わりに、あなたはプログラム的に素晴らしい画像モザイクを作成するためにアスポスを使用するための包括的な理解を得るでしょう.

原則

コードに入る前に、以下の前提条件が設定されていることを確認してください:

  • Visual Studio 2019 またはそれ以降
  • .NET 6.0 またはそれ以降(または .Net Framework 4.6.2+)
  • Aspose.Imaging for .NET から NuGet

コード 例:自動化画像モザイク レイアウト C#

using System;
using System.Collections.Generic;
using System.Drawing;
using Aspose.Imaging;
using Aspose.Imaging.FileFormats;

public class ImageMosaicGenerator
{
    public static void Main(string[] args)
    {
        // Initialize metered license
        Metered metered = new Metered();
        metered.SetMeteredKey("your-public-key", "your-private-key");

        string inputFolder = @"path\to\input\images";
        int rows = 5;
        int columns = 4;

        List<string> imagePaths = GetImageFilePaths(inputFolder);
        
        // Calculate output canvas dimensions
        int totalWidth = columns * 200; // Assuming each image is resized to 200x200 pixels
        int totalHeight = rows * 200;
        
        using (Bitmap compositeImage = new Bitmap(totalWidth, totalHeight))
        {
            Graphics graphics = Graphics.FromImage(compositeImage);
            
            for (int i = 0; i < imagePaths.Count; i++)
            {
                string imagePath = imagePaths[i];
                
                // Load and resize images
                Image image = Image.Load(imagePath);
                int newWidth = 200;
                int newHeight = 200;

                if (image.VerticalResolution != 96 || image.HorizontalResolution != 96)
                {
                    image.ResizeFullFrame(newWidth, newHeight, ResizeType.Bicubic);
                }
                
                // Calculate position in grid
                int xPosition = (i % columns) * newWidth;
                int yPosition = (i / columns) * newHeight;

                graphics.DrawImage(image, xPosition, yPosition);
            }

            // Save the composite image
            string outputFilePath = @"path\to\output\mosaic.jpg";
            compositeImage.Save(outputFilePath);

            Console.WriteLine($"Mosaic layout saved to {outputFilePath}");
        }
    }

    private static List<string> GetImageFilePaths(string folderPath)
    {
        // Implement logic to get all image file paths from the specified folder
        return new List<string>();
    }
}

コードの理解

この実施の重要な部分を解き明かしましょう:

ステップ1:初期設定

まず、測定ライセンスを開始し、入力ファイルを充電します:

// Initialize metered license
Metered metered = new Metered();
metered.SetMeteredKey("your-public-key", "your-private-key");

string inputFolder = @"path\to\input\images";
int rows = 5;
int columns = 4;

List<string> imagePaths = GetImageFilePaths(inputFolder);

このセクションでは、必要なライセンスを設定し、指定されたフォルダーからすべての画像をリストにアップロードします.

ステップ2:出力カンバスサイズの計算

次に、列と列の数に基づいて出力カンバの次元を計算します:

int totalWidth = columns * 200; // Assuming each image is resized to 200x200 pixels
int totalHeight = rows * 200;

ここでは、ネットワークの次元に基づいて構成画像の幅と高さを決定します.

ステップ3:画像のアップロードとリサイクル

現在、リストからそれぞれの画像をアップロードし、ネットワーク内で適合するように再編します:

using (Bitmap compositeImage = new Bitmap(totalWidth, totalHeight))
{
    Graphics graphics = Graphics.FromImage(compositeImage);
    
    for (int i = 0; i < imagePaths.Count; i++)
    {
        string imagePath = imagePaths[i];
        
        // Load and resize images
        Image image = Image.Load(imagePath);
        int newWidth = 200;
        int newHeight = 200;

        if (image.VerticalResolution != 96 || image.HorizontalResolution != 96)
        {
            image.ResizeFullFrame(newWidth, newHeight, ResizeType.Bicubic);
        }
    }
}

このスナイプはそれぞれの画像を積み重ね、ネットワークに置く前に統一サイズにリサイクルします.

ステップ4:グリッドで画像を整理する

その後、ネットワーク内の各画像の位置を計算し、それを使用して描きます Graphics.DrawImage:

// Calculate position in grid
int xPosition = (i % columns) * newWidth;
int yPosition = (i / columns) * newHeight;

graphics.DrawImage(image, xPosition, yPosition);

この部分は、それぞれの画像がそのインデックスに基づいて構成された画像の中に正しく置かれていることを保証します.

ステップ5:コンポーネントイメージの保存

最後に、複合画像を指定された出力ルートに保存します:

// Save the composite image
string outputFilePath = @"path\to\output\mosaic.jpg";
compositeImage.Save(outputFilePath);

Console.WriteLine($"Mosaic layout saved to {outputFilePath}");

このスナイプは、最後のモザイク画像をファイルに保存します.

結論

このブログの投稿では、Aspose.Imaging for .NET を使用して画像モザイクを作成するプロセスを自動化する方法を調べました このガイドで示されたステップに従って、一貫性と視覚的に魅力的なネットワークレイアウトをプログラム的に簡単に作成することができます このアプローチは時間を節約するだけでなく、プロジェクトが高品質と一致性を維持することを保証します.

あなたの特定のニーズにソリューションをカスタマイズするために、さまざまな構成とアップグレードで実験する自由を感じる!

More in this category