tags:

views:

60

answers:

4

i want to move the image from one point to another.what class(es) are needed for this

+1  A: 

you can use PictureBox to load an image, then change PictureBox location to move it.

serhio
can i take many picture into a singe picture box
Chunmun Tyagi
@ShaShank: only 1 Picture and only for WinForms
Henk Holterman
@shashank: use multiple pictureBoxes if you want.
serhio
A: 

You can use PictureBox by changing the location property, or you can use DrawImage by using the Paint event or override the OnPaint method. The second option is a little bit harder but work faster on runtime.

Manu
this is for WinForms
Manu
A: 

A winforms answer.

The best way might be to have single picturebox that is associated with an imagelist. You can load lots of different images into the imagelist and then simply set the picturebox to work off the imagelist. You will change the image by changing the picturebox.imageindex.

As mentioned above, to change the location of the picturebox, you can change the picturebox.location.

This should allow you to do what you want!

Ben Cawley
A: 

Let's add functionality to the form by allowing the user to move it. To do that, we must handle events for the MouseDown, MouseMove and MouseUp actions. In the Designer View, select the first PictureBox, the bigger one, and go to the Properties window (or press F4), and click the lightning shaped button. In the Mouse section, double-click MouseDown property. Visual Studio has created for you a function to handle the MouseDown action on the PictureBox (NOT on the form):

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { } We'll need to declare a variable to see weather the mouse is pressed or not (I called it mouse_is_down, you can call it however you want). We must declare this variable, after the class definition, right under the declaration of the two PictureBox controls. Add the following code:

private bool mouse_is_down=false; Now, go to pictureBox1_MouseDown method and add the following:

mouse_is_down=true; We now know when the mouse is down. But if the user presses the mouse and then releases it, our mouse_is_down remains true. Let's fix that by adding another function that handles the MouseUp event. In the Mouse section, double-click MouseUp property, and add the following code:

mouse_is_down=false; So far, so good. Let's handle now the MouseMove action. Double-click the MouseMove action. We are now in the pictureBox1_MouseMove method. If the mouse is down, and the user wants to move the window, we would like that to move around. Add the following code: if ( mouse_is_down ) { Point current_pos = Control.MousePosition; this.Location = current_pos; } The variable of type Point represents the current position of the mouse. Let's run the application and see what's happening.

      As you can see, the window moves, but the mouse position is set to the upper left corner of the window. Why is that? By changing the mouse position, we also change the window position and the mouse coordinates are set to (0,0)(relative to the window) . This can be avoided by adding some extra code. All we have to do is remember the position of the mouse when the click was performed. We'll need to declare another variable, of type Point, at the beginning of the class. I called it mouse_pos.

private Point mouse_pos; In the pictureBox1_MouseDown method, add the following code to remember the mouse position when the click was performed:

mouse_pos.X = e.X; mouse_pos.Y = e.Y; Let's go now to the pictureBox1_MouseMove method, and add:

if ( mouse_is_down ) { Point current_pos = Control.MousePosition; current_pos.X = current_pos.X - mouse_pos.X; //add this current_pos.Y = current_pos.Y - mouse_pos.Y; //add this this.Location = current_pos; } We now added code to remember the mouse position. Run the application.

      In the end, for some fun, let's add some improvements:
  • in the pictureBox1_MouseDown method, add the following line:

pictureBox1.Image=new Bitmap("..\..\Images\up bar selected.jpg"); What this basically does is when you perform the click, changes the Image property of pictureBox1.

  • in the PictureBox_MouseUp method, let's change back the image:

pictureBox1.Image =new Bitmap("..\..\Images\up bar.jpg");

".." indicates the current directory's parent directory, "\" indicates an escape sequence, and must be typed twice("\") to indicate the "\" character. When you run your application, your current directory is "{path to application}\bin\Debug", and to access the Image folder, I have to "climb" two directories up.