如何将图像从左到右切成相等的部分?
How to cut image into equal parts left to right?
假设我有 1 张图片,我正在寻找一种方法,将它从左到右分成相等的部分,我有这段代码,但是它被切成很长的部分:
var image = pictureBox1.Image;
var splitInto = 12;
using (var originalImage = new Bitmap(image))
{
for (int i = 0; i < splitInto; i++)
{
var rect = new Rectangle(0, originalImage.Height / splitInto * i, originalImage.Width, originalImage.Height / splitInto);
using (var clonedImage = originalImage.Clone(rect, originalImage.PixelFormat))
clonedImage.Save(directory + $"\PageImage{i + 1}.jpg");
}
}
我正在寻找这样的结果:
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]
每个方块都连接到图像的地方我想过 2 个 for 循环,但我想不通。
Original
Edited
图像是双向对象。你不要说你的 12 件是 2 行和 6 列还是 3 行和 4 列(或......)。所以首先,你应该有 2 个参数;在你的“像这样”模式中,你似乎可以设置:
int splitX = 4;
int splitY = 3;
而且,是的,有两个嵌套循环比只有一个循环要简单和自然得多。
这是一个可能的解决方案:
var image = pictureBox1.Image;
var splitX = 4;
var splitY = 3;
using (var originalImage = new Bitmap(image))
{
var incX = originalImage.Width / splitX;
var incY = originalImage.Height / splitY;
var startX = 0;
for (int i = 0; i < splitX; i++)
{
var startY = 0;
for (int j = 0; j < splitY; j++)
{
var rect = new Rectangle(startX, startY, incX, incY);
using (var clonedImage = originalImage.Clone(rect, originalImage.PixelFormat))
clonedImage.Save(directory + $"\PageImage{i + 1}{j + 1}.jpg");
startY += incY;
}
startX += incX;
}
}
假设我有 1 张图片,我正在寻找一种方法,将它从左到右分成相等的部分,我有这段代码,但是它被切成很长的部分:
var image = pictureBox1.Image;
var splitInto = 12;
using (var originalImage = new Bitmap(image))
{
for (int i = 0; i < splitInto; i++)
{
var rect = new Rectangle(0, originalImage.Height / splitInto * i, originalImage.Width, originalImage.Height / splitInto);
using (var clonedImage = originalImage.Clone(rect, originalImage.PixelFormat))
clonedImage.Save(directory + $"\PageImage{i + 1}.jpg");
}
}
我正在寻找这样的结果:
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]
[ ] [ ] [ ] [ ]
每个方块都连接到图像的地方我想过 2 个 for 循环,但我想不通。
Original
Edited
图像是双向对象。你不要说你的 12 件是 2 行和 6 列还是 3 行和 4 列(或......)。所以首先,你应该有 2 个参数;在你的“像这样”模式中,你似乎可以设置:
int splitX = 4;
int splitY = 3;
而且,是的,有两个嵌套循环比只有一个循环要简单和自然得多。
这是一个可能的解决方案:
var image = pictureBox1.Image;
var splitX = 4;
var splitY = 3;
using (var originalImage = new Bitmap(image))
{
var incX = originalImage.Width / splitX;
var incY = originalImage.Height / splitY;
var startX = 0;
for (int i = 0; i < splitX; i++)
{
var startY = 0;
for (int j = 0; j < splitY; j++)
{
var rect = new Rectangle(startX, startY, incX, incY);
using (var clonedImage = originalImage.Clone(rect, originalImage.PixelFormat))
clonedImage.Save(directory + $"\PageImage{i + 1}{j + 1}.jpg");
startY += incY;
}
startX += incX;
}
}