Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut an Image into 9 pieces C# [duplicate]

Possible Duplicate:
Image splitting into 9 pieces

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

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

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.

Thanks in advance.

like image 979
whoone Avatar asked Nov 29 '12 12:11

whoone


People also ask

How do you cut a large picture into a grid of smaller pictures?

Create one big slice, containing the whole image. Right click on the slice icon in the top left corner of the image. Choose Divide Slice and define into how many slices do you want to divide your image, or set the size of each sub-slice. Hit ok.


2 Answers

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:

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];
...
like image 126
Øyvind Bråthen Avatar answered Sep 30 '22 12:09

Øyvind Bråthen


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

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];
like image 24
Nikola Davidovic Avatar answered Sep 30 '22 12:09

Nikola Davidovic