Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
Please ant one suggest a idea to divide an image in to multiple 8*8 tiles in C#.
I tried in several ways, but couldn't get any ideas.

Thanks in Adavnce
Posted
Comments
Kenneth Haugland 24-Nov-15 5:58am    
Show us some code you tried, that failed?
BillWoodruff 24-Nov-15 9:18am    
split into units of what ? 8 pixels, 8 ?

I'm surprised you couldn't get any idea, I googled "crop image c#" and found lots of sample code.

crop image c# [^]
 
Share this answer
 
Hello Kasthuri,
I have developed simple 3 x 3 slider puzzle game and here is the method that should give You an idea how to divide image into 3 x 3 tiles. If You analize it, there should not be a problem to change program code to divide image to 8 x 8 tiles.

Original free open source program code is at this Url adress :
Game Puzzle - Perić Željko Smederevo[^]

C#
void Initialize_Tiles()
      {
          //
          // Cut original image to nine rectangular peaces
          // that have identical dimensions and set them to
          // the elements of table 'Puzzle_Tiles'.
          //
          // Placed by it's indexes on fixed positions
          // inside Puzzle_Table, in the below shown order,
          // they constitute original image.
          //
          //       1 2 3
          //       4 5 6
          //       7 8 9
          //

          // Original image
          Bitmap Original_Image;

          // Original image pixel format
          PixelFormat Pixel_Format;

          // Tile rectangle
          RectangleF Tile;

          int Tile_Height;// Tile height
          int Tile_Width; // Tile width

          // Set new original image and it's pixel format
          Original_Image = new Bitmap(BackgroundImage);
          Pixel_Format = Original_Image.PixelFormat;

          // Set new Tile
          Tile_Height = Original_Image.Height/3;
          Tile_Width = Original_Image.Width/3;

          Tile = new RectangleF();

          Tile.Width  = Tile_Width;
          Tile.Height = Tile_Height;

          //
          // Set values to elements of table Puzzle_Tiles
          //

          // 0. Tile, empty tile
          Puzzle_Tiles[0].Image = null;

          // 1. Tile
          Tile.X = 0;
          Tile.Y = 0;
          Puzzle_Tiles[1].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 2. Tile
          Tile.X = Tile_Width;
          Tile.Y = 0;
          Puzzle_Tiles[2].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 3. Tile
          Tile.X = 2*Tile_Width;
          Tile.Y = 0;
          Puzzle_Tiles[3].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 4. Tile
          Tile.X = 0;
          Tile.Y = Tile_Height;
          Puzzle_Tiles[4].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 5. Tile
          Tile.X = Tile_Width;
          Tile.Y = Tile_Height;
          Puzzle_Tiles[5].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 6. Tile
          Tile.X = 2*Tile_Width;
          Tile.Y = Tile_Height;
          Puzzle_Tiles[6].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 7. Tile
          Tile.X = 0;
          Tile.Y = 2*Tile_Height;
          Puzzle_Tiles[7].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 8. Tile
          Tile.X = Tile_Width;
          Tile.Y = 2*Tile_Height;
          Puzzle_Tiles[8].Image = Original_Image.Clone(Tile,Pixel_Format);

          // 9. Tile
          Tile.X = 2*Tile_Width;
          Tile.Y = 2*Tile_Height;
          Puzzle_Tiles[9].Image = Original_Image.Clone(Tile,Pixel_Format);
      }


As You can notice there is a Table Puzzle_Tiles, one dimenzional Array, type of PictureBox with its instance Image, that is used for memorizing smaler peaces of original image. Its declaration is at the begining of main class and it is a global variable :

// Table 'Puzzle_Tiles'  contains nine
// pictures cropped from original image.
PictureBox [] Puzzle_Tiles;


And initialization at Initialize_Tables method see original program code at Game Puzzle - Perić Željko Smederevo[^] :

C#
int position;

// Table 'Puzzle_Tiles'  contains nine
// pictures cropped from original image.
Puzzle_Tiles = new PictureBox[10];
Puzzle_Tiles.Initialize();

// Set initial values for table elements
position = 0;
for(position=0;position<10;position++)
{
    Puzzle_Tiles[position] = new PictureBox();
    Puzzle_Tiles[position].Image = null;
}


I think that this is enough for You to solve the problem.
All the best,
Željko Perić
 
Share this answer
 
Comments
F-ES Sitecore 7-Nov-17 12:03pm    
Check the year questions were posted before spending too much time and effort giving an answer!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900