07-21-2013, 05:01 PM
The First thing to do is to load your images, you could use PictureBox
If you chose a PictureBox:, add handles to your picture box(in this case a MouseClick event).
Since the picture has lots of sub-pictures you need to calculate the picture location on the huge image.
A simple way is width and height of the image(just like matrix in math).
So lets represent the image in to matrix of titles(the image is the matrix and sub-images are numbers of the matrix):
01 02 03 04 05
06 07 08 09 10
11 12 13 14 15
16 17 18 79 20
21 22 23 24 25
This matrix is made of 5 width in titles and 5 height in title( lets call it Images[5][5]).
Here is a sample code for getting a title on a certain location:
//defining the title width and height in pixels
int title_Width = 20;
int title_height= 20;
//our image
Bitmap bmp = null;
private void pictureBox1_MouseClick(object sender, MouseEventArgs e){
//getting the mouse location on the image
int mouse_Location_x = e.X;
int mouse_Location_y = e.Y;
//seeing in what title I clicked
int title_x = (mouse_Location_x / title_Width) - 1;
int title_y = mouse_Location_y / title_height - 1;
//creating a Rectangle for the image
Rectangle rect = new Rectangle(title_Width*title_x, title_height*
title_y ,title_Width, title_height );
//getting the image
bmp = new Bitmap(title_Width, title_height);
pictureBox1.DrawToBitmap(bmp, rect);
}
Or you can get the images on load and store them into a matrix then after you found x and y of the image the get the image from the matrix.
Bitmap [][] images = null;
private void Form1_Load(object sender, EventArgs e){
//creating images
images = new Bitmap[MAX_X][MAX_Y];
for(int i =0; i < nr_rows; i++){
for(int j =0; j < nr_columns; j++){
//creating a Rectanglele for the image
Rectangle rect = new Rectangle(title_Width*j, title_height*i ,title_Width, title_height );
images[i][j] = new Bitmap(title_Width, title_height);
pictureBox1.DrawToBitmap(images[i][j], rect);
}
}
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e){
//getting the mouse location on the image
int mouse_Location_x = e.X;
int mouse_Location_y = e.Y;
//seeing in what title I clicked
int title_x = (mouse_Location_x / title_Width) - 1;
int title_y = mouse_Location_y / title_height - 1;
//getting the images
bmp = images[title_x][title_y];
}
This is just an example, you can get the title with and height by knowing the image width and height, and divide with the number of columns and rows
Another method is to load the image into a bitmap, then then get the subimages, then use a flowlayoutpanel and add your picturebox maually, with a onClick event
Bitmap bmp = null;
private void Form1_Load(object sender, EventArgs e){
Bitmap BigImage = Bitmap.FromFile("MyImage.bmp")
//creating images
images = new Bitmap[MAX_X][MAX_Y];
for(int i =0; i < nr_rows; i++){
for(int j =0; j < nr_columns; j++){
//creating a Rectanglele for the image
Rectangle rect = new Rectangle(title_Width*j, title_height*i ,title_Width, title_height );
images[i][j] = new Bitmap(title_Width, title_height);
pictureBox1.DrawToBitmap(images[i][j], rect);
PictureBox pb = new PictureBox ();
pb.Click += new System.EventHandler(myImage_click);
pb.Image = images[i][j] ;
pb.visible = true;
flowLayoutPanel1.Controls.Add(pb);
}
}
}
private void myImage_click(object sender, EventArgs e){
PictureBox pb = (PictureBox)sender;
bmp = (Bitmap)pb.Image;
}

