Загрузка данных


using System;
using System.Drawing;

public class AtlasParser
{
    private readonly int columns;
    private readonly int rows;
    private readonly Bitmap[] sprites;

    public AtlasParser(int columns, int rows, string atlasPath)
    {
        if (columns <= 0 || rows <= 0)
            throw new ArgumentException("Количество строк и столбцов должно быть положительным числом");
        
        if (string.IsNullOrEmpty(atlasPath))
            throw new ArgumentNullException(nameof(atlasPath));
        
        this.columns = columns;
        this.rows = rows;
        this.sprites = new Bitmap[columns * rows];

        using (Bitmap atlasBitmap = new Bitmap(atlasPath))
        {
            int spriteWidth = atlasBitmap.Width / this.columns;
            int spriteHeight = atlasBitmap.Height / this.rows;

            for (int r = 0; r < rows; r++)
            {
                for (int c = 0; c < columns; c++)
                {
                    int index = r * columns + c;
                    Bitmap sprite = new Bitmap(spriteWidth, spriteHeight);
                    
                    using (Graphics graphics = Graphics.FromImage(sprite))
                    {
                        Rectangle spriteRectangle = new Rectangle(
                            c * spriteWidth,
                            r * spriteHeight,
                            spriteWidth,
                            spriteHeight
                        );
                        
                        graphics.DrawImage(atlasBitmap, 0, 0, spriteRectangle, GraphicsUnit.Pixel);
                    }
                    
                    this.sprites[index] = sprite;
                }
            }
        }
    }

    public Bitmap GetSprite(int index)
    {
        if (index < 0 || index >= sprites.Length)
            throw new ArgumentOutOfRangeException(nameof(index), "Индекс спрайта вне допустимого диапазона");
        
        return this.sprites[index];
    }

    public Bitmap GetSprite(int row, int col)
    {
        if (row < 0 || row >= rows)
            throw new ArgumentOutOfRangeException(nameof(row), "Строка вне допустимого диапазона");
        
        if (col < 0 || col >= columns)
            throw new ArgumentOutOfRangeException(nameof(col), "Столбец вне допустимого диапазона");
        
        return this.sprites[row * this.columns + col];
    }
    
    public int TotalSprites => sprites.Length;
    public int Columns => columns;
    public int Rows => rows;
}