![]() |
|
IconPicker - Printable Version +- LCKB (https://lckb.dev/forum) +-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109) +--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196) +---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192) +---- Thread: IconPicker (/showthread.php?tid=2231) Pages:
1
2
|
- soryjero - 07-21-2013 hello, this time my question is more broad because its first time i try this. i am trying to make a icon picker but i dont know how to do it. i only know i need a picture box so i have it. 2 i also want to make a new form where i can click on icon to choose it, so i have it too. 2 and i also have images in /Images 2 but i dont know how to code an icon picker, so could anyone help me or give me a piece or code? thanks in advance - someone - 07-21-2013 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; } - soryjero - 07-21-2013 thanks someone. when i have some time, i will try it and i will post here the result. - soryjero - 07-24-2013 i have loaded image and everything is perfect, but when i try to click with mouse nothing happens. i have written what someone told me: //defining the title width and height in pixels int title_Width = 32; int title_height = 32; //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); } but when i click nothing happens, so i dont know what are wrong because this function clearly draws a rectangle. P.S. i have loaded image in a bitmap. Bitmap myImage = new Bitmap("Images/ItemBtn" + comboBox1.SelectedIndex + ".jpg"); pictureBox1.ClientSize = new Size(512, 512); pictureBox1.Image = (Image)myImage; - someone - 07-24-2013 Change to this: //seeing in what title I clicked int title_x = (mouse_Location_x / title_Width); int title_y = (mouse_Location_y / title_height); And to this: //getting the image bmp = new Bitmap(title_Width, title_height); Bitmap temp = new Bitmap(pictureBox1.Image); bmp = temp.Clone(rect, temp.PixelFormat); temp.Dispose(); - soryjero - 07-24-2013 Change to this: //seeing in what title I clicked int title_x = (mouse_Location_x / title_Width); int title_y = (mouse_Location_y / title_height); And to this: //getting the image bmp = new Bitmap(title_Width, title_height); Bitmap temp = new Bitmap(pictureBox1.Image); bmp = temp.Clone(rect, temp.PixelFormat); temp.Dispose(); thanks, but i have replaced those parts of codes and doesnt work yet ![]() - someone - 07-24-2013 Are you sure you added the MouseClick event handle to the picturebox? not the Click event handle - Wizatek - 07-24-2013 This is what i use. 2 - soryjero - 07-24-2013 Are you sure you added the MouseClick event handle to the picturebox? not the Click event handle sure. 2 This is what i use. 2 i cant download it. i get an error. 2 - Wizatek - 07-25-2013 2 |