如何将图像分割成等大小
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
}
}
}
}
答案 0 :(得分: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);
}
}
处理按钮代码:拆分图片&amp;保存或使用
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
}
}
}
}
我认为你无意中使用了#34; j * one_img_w, i * one_img_h
&#34;在矩形
所以它将行转换为列和列转换为行