使用 C# 将图像拆分为行 x 列大小相等的图像矩阵

Split Image as Rows x Columns equal sized Image Matrix Using C#

how to split an image into Equal Size 3x3,3x4,4x4,6x6,4x6,6x8 something like that

Conceptualize by given Image:将简单的图像转换成那种形式,

在给定的答案中,它是一种通用算法,可以将图像拆分为适当缩放的 行 x 列大小相等的图像矩阵 它给出了像

1 4 7
2 5 8
3 6 9

我使用了以下代码....但它并不完美

加载按钮代码:将图像加载到变量 image1

Image image1;
private void btn_Open_File_BG_Click(object sender, EventArgs e)
{
    OpenFileDialog openDialog = new OpenFileDialog();
    if (openDialog.ShowDialog() == DialogResult.OK)
    {
        image1 = new Bitmap(openDialog.FileName);
    }
}

处理按钮代码:拆分图片并保存或使用

private void Img_BG_process_Click(object sender, EventArgs e)
        {
            int rows = 5;//No of Rows as per Desire
            int columns = 6;//No of columns as per Desire
            var imgarray = new Image[rows, columns];//Create Image Array of Size Rows X Colums
            var img = image1;//Get Image from anywhere, From File Or Using Dialogbox used previously
            int height = img.Height;
            int width = img.Width;//Get image Height & Width of Input Image
            int one_img_h = height / rows;
            int one_img_w = width / columns;//You need Rows x Columns, So get 1/rows Height, 1/columns width of original Image
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    imgarray[i, j] = new Bitmap(one_img_w, one_img_h);//generating new bitmap
                    var graphics = Graphics.FromImage(imgarray[i, j]);
                    graphics.DrawImage(img, new Rectangle(0, 0, one_img_w, one_img_h), new Rectangle(i * one_img_w, j * one_img_h, one_img_w, one_img_h), GraphicsUnit.Pixel);//Generating Splitted Pieces of Image
                    graphics.Dispose();
                }
            }
//Image Is spitted You can use it by getting image from **imgarray[Rows, Columns]**
//Or You can Save it by using Following Code


            var destinationFolderName = "";//Define a saving path
            FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
            DialogResult result = FolderBrowserDialog1.ShowDialog();//Get folder Path Where splitted Image Saved
            if (result == DialogResult.OK)
            {
                destinationFolderName = FolderBrowserDialog1.SelectedPath;
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        imgarray[i, j].Save(@"" + destinationFolderName + "/Image_" + i + "_" + j + ".jpg");//Save every image in Array [row][column] on local Path
                    }
                }
            }
        } 

加载按钮代码:将图像加载到变量 image1

Image image1;
private void btn_Open_File_BG_Click(object sender, EventArgs e)
{
    OpenFileDialog openDialog = new OpenFileDialog();
    if (openDialog.ShowDialog() == DialogResult.OK)
    {
        image1 = new Bitmap(openDialog.FileName);
    }
}

处理按钮代码:拆分图片并保存或使用

private void Img_BG_process_Click(object sender, EventArgs e)
        {
            int rows = 5;//No of Rows as per Desire
            int columns = 6;//No of columns as per Desire
            var imgarray = new Image[rows, columns];//Create Image Array of Size Rows X Colums
            var img = image1;//Get Image from anywhere, From File Or Using Dialogbox used previously
            int height = img.Height;
            int width = img.Width;//Get image Height & Width of Input Image
            int one_img_h = height / rows;
            int one_img_w = width / columns;//You need Rows x Columns, So get 1/rows Height, 1/columns width of original Image
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    imgarray[i, j] = new Bitmap(one_img_w, one_img_h);//generating new bitmap
                    var graphics = Graphics.FromImage(imgarray[i, j]);
                    graphics.DrawImage(img, new Rectangle(0, 0, one_img_w, one_img_h), new Rectangle(j * one_img_w, i * one_img_h, one_img_w, one_img_h), GraphicsUnit.Pixel);//Generating Splitted Pieces of Image
                    graphics.Dispose();
                }
            }
//Image Is spitted You can use it by getting image from **imgarray[Rows, Columns]**
//Or You can Save it by using Following Code


            var destinationFolderName = "";//Define a saving path
            FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
            DialogResult result = FolderBrowserDialog1.ShowDialog();//Get folder Path Where splitted Image Saved
            if (result == DialogResult.OK)
            {
                destinationFolderName = FolderBrowserDialog1.SelectedPath;
                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < columns; j++)
                    {
                        imgarray[i, j].Save(@"" + destinationFolderName + "/Image_" + i + "_" + j + ".jpg");//Save every image in Array [row][column] on local Path
                    }
                }
            }
        }

我认为您在 Rectangle

中无意中使用了“j * one_img_w, i * one_img_h

因此它将行转换为列,将列转换为行