将图像切成 9 块 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13625891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:14:45  来源:igfitidea点击:

Cut an Image into 9 pieces C#

c#imagepicturebox

提问by whoone

Possible Duplicate:
Image splitting into 9 pieces

可能的重复:
图像分成 9 块

Though I googled enough but unfortunately failed to find a help. This Code Project Tutorialalso failed to serve me what I actually need.

虽然我用谷歌搜索了足够多但不幸的是未能找到帮助。这个代码项目教程也未能满足我的实际需要。

I have an Image and 9 PictureBox(s) in a WinForm.

我在 WinForm 中有一个图像和 9 个图片框。

Image img = Image.FromFile("media\a.png"); // a.png has 312X312 width and height
//          some code help, to get
//          img1, img2, img3, img4, img5, img6, img7, img8, img9
//          having equal width and height
//          then...
pictureBox1.Image = img1;
pictureBox2.Image = img2;
pictureBox3.Image = img3;
pictureBox4.Image = img4;
pictureBox5.Image = img5;
pictureBox6.Image = img6;
pictureBox7.Image = img7;
pictureBox8.Image = img8;
pictureBox9.Image = img9;

Here is an example Image for you:

这是您的示例图像:

enter image description here

在此处输入图片说明

This is a part of my Picture Puzzle class project. I have done with photoshop images, now want to dynamically cut.

这是我的图片拼图课程项目的一部分。我已经完成了photoshop图像,现在想动态剪切。

Thanks in advance.

提前致谢。

采纳答案by ?yvind Br?then

First off, instead of using img1, img2... use an array with a size of 9. Then it's much easier to do this using a couple of loops like this:

首先,不要使用 img1、img2... 使用大小为 9 的数组。然后使用这样的几个循环更容易做到这一点:

var imgarray = new Image[9];
var img = Image.FromFile("media\a.png");
for( int i = 0; i < 3; i++){
  for( int j = 0; j < 3; j++){
    var index = i*3+j;
    imgarray[index] = new Bitmap(104,104);
    var graphics = Graphics.FromImage(imgarray[index]);
    graphics.DrawImage( img, new Rectangle(0,0,104,104), new Rectangle(i*104, j*104,104,104), GraphicsUnit.Pixel);
    graphics.Dispose();
  }
}

Then you can fill your boxes like this:

然后你可以像这样填充你的盒子:

pictureBox1.Image = imgarray[0];
pictureBox2.Image = imgarray[1];
...

回答by Nikola Davidovic

You could try with this code. It basically creates a matrix of images (like you need in your project) and draws on each Bitmapadequate part of the large image. The same concept you could use for the pictureBoxesand put them in the matrix.

您可以尝试使用此代码。它基本上创建了一个图像矩阵(就像您在项目中需要的那样)并Bitmap在大图像的每个适当部分上进行绘制。您可以使用相同的概念pictureBoxes并将它们放入矩阵中。

Image img = Image.FromFile("media\a.png"); // a.png has 312X312 width and height
int widthThird = (int)((double)img.Width / 3.0 + 0.5);
int heightThird = (int)((double)img.Height / 3.0 + 0.5);
Bitmap[,] bmps = new Bitmap[3,3];
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    {
        bmps[i, j] = new Bitmap(widthThird, heightThird);
        Graphics g = Graphics.FromImage(bmps[i, j]);
        g.DrawImage(img, new Rectangle(0, 0, widthThird, heightThird), new Rectangle(j * widthThird, i * heightThird, widthThird, heightThird), GraphicsUnit.Pixel);
        g.Dispose();
    }
pictureBox1.Image = bmps[0, 0];
pictureBox2.Image = bmps[0, 1];
pictureBox3.Image = bmps[0, 2];
pictureBox4.Image = bmps[1, 0];
pictureBox5.Image = bmps[1, 1];
pictureBox6.Image = bmps[1, 2];
pictureBox7.Image = bmps[2, 0];
pictureBox8.Image = bmps[2, 1];
pictureBox9.Image = bmps[2, 2];