如何在 C# 中将图像分割成更小的部分?
How to split an image into smaller pieces in C#?
如何将一个图像分割成多个子图像?比如,我是否必须读取像素并将其以某种方式转换为图像?
例如:
如果图像的尺寸为 100px(宽度)和 180px(高度),并且我想将其拆分为 4x4,我是否会读取前 25px 的宽度和前 45px 的高度和然后正确地增加它?
如果是这样,我会将像素存储到什么位置?更具体地说,它会被保存为字节数组、图像等吗?
您可以尝试以下代码示例(摘自 );
for (int i = 0; i < 4; i++)
{
for (int y = 0; y < 4; y++)
{
Rectangle r = new Rectangle(i*(pictureBox1.Image.Width / 4),
y*(pictureBox1.Image.Height / 4),
pictureBox1.Image.Width / 4,
pictureBox1.Image.Height / 4);
g.DrawRectangle(pen,r );
list.Add(cropImage(pictureBox1.Image, r));
}
}
另一种方法是使用 BitMap.Clone
,您可以在下面的 link.
中找到示例
使用 Bitmap class to hold the image and its Clone
method to cut out your rectangles of arbitrary size. As a Bitmap
it comes with several convenience methods such as Save 此重载将保存到流中,另一个允许您将其保存到文件中。
如何将一个图像分割成多个子图像?比如,我是否必须读取像素并将其以某种方式转换为图像?
例如:
如果图像的尺寸为 100px(宽度)和 180px(高度),并且我想将其拆分为 4x4,我是否会读取前 25px 的宽度和前 45px 的高度和然后正确地增加它?
如果是这样,我会将像素存储到什么位置?更具体地说,它会被保存为字节数组、图像等吗?
您可以尝试以下代码示例(摘自 );
for (int i = 0; i < 4; i++)
{
for (int y = 0; y < 4; y++)
{
Rectangle r = new Rectangle(i*(pictureBox1.Image.Width / 4),
y*(pictureBox1.Image.Height / 4),
pictureBox1.Image.Width / 4,
pictureBox1.Image.Height / 4);
g.DrawRectangle(pen,r );
list.Add(cropImage(pictureBox1.Image, r));
}
}
另一种方法是使用 BitMap.Clone
,您可以在下面的 link.
使用 Bitmap class to hold the image and its Clone
method to cut out your rectangles of arbitrary size. As a Bitmap
it comes with several convenience methods such as Save 此重载将保存到流中,另一个允许您将其保存到文件中。