Разделить изображение как строки x столбцы одинакового размера Матрица изображения с использованием С#

как разделить изображение на равные по размеру 3x3,3x4,4x4,6x6,4x6,6x8 что-то в этом роде

Концептуализация с помощью данного изображения: преобразуйте простое изображение в эту форму, введите описание изображения здесь

В заданном ответе это алгоритм общего назначения для разделения изображения на правильно масштабированные строки 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
                    }
                }
            }
        } 

person Khurram Sharif    schedule 18.01.2015    source источник


Ответы (1)


Код кнопки загрузки: Загрузить изображение в переменную 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
                    }
                }
            }
        }

Я думаю, что вы случайно используете "j * one_img_w, i * one_img_h" в Rectangle.

ТАК он преобразует строки в столбцы и столбцы в строки

person Community    schedule 01.06.2015