Manipulating drag and drop images in C # form windows

I am trying to write a program that will allow the user to drag and drop images into the program, and then be able to select an image, move it, resize, crop, etc.

So far I have created a window form consisting of a panel. The user can drag the image file onto the panel, and the image will be created in the coordinates of the mouse when it is reset, and the image will be loaded into the image window. I can add multiple images this way.

Now I want to allow the user to manipulate and move the images that they dropped into the panel.

I tried to find solutions, but did not seem to find an answer that I understand.

Any help is greatly appreciated.

This is my current code.

private void panel1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.All; } private void panel1_DragDrop(object sender, DragEventArgs e) { String[] imagePaths = (String[])e.Data.GetData(DataFormats.FileDrop); foreach (string path in imagePaths) { Point point = panel1.PointToClient(Cursor.Position); PictureBox pb = new PictureBox(); pb.ImageLocation = path; pb.Left = point.X; pb.Top = point.Y; panel1.Controls.Add(pb); //g.DrawImage(Image.FromFile(path), point); } } 
+4
source share
1 answer

You can get the mouse position when the user first clicks and then tracks the mouse position in the PictureBox MouseMove . You can attach these handlers to several PictureBoxes.

 private int xPos; private int yPos; private void pb_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { xPos = eX; yPos = eY; } } private void pb_MouseMove(object sender, MouseEventArgs e) { PictureBox p = sender as PictureBox; if(p != null) { if (e.Button == MouseButtons.Left) { p.Top += (eY - yPos); p.Left += (eX - xPos); } } } 

For dynamic PictureBoxes, you can attach handlers like this

 PictureBox dpb = new PictureBox(); dpb.MouseDown += pb_MouseDown; dbp.MouseMove += pb_MouseMove; //fill the rest of the properties... 
+1
source

Source: https://habr.com/ru/post/1489904/


All Articles