操作windows窗体c中的拖放图像
本文关键字:拖放 图像 windows 窗体 操作 | 更新日期: 2023-09-27 18:21:24
我正在尝试编写一个程序,该程序将允许用户将图像拖放到程序中,然后能够选择图像、移动图像、重新调整图像大小、裁剪图像等。
到目前为止,我已经创建了一个由面板组成的窗口窗体。用户可以将图片文件拖动到面板上,当鼠标放下并在图片框中加载图像时,将在鼠标坐标处创建一个图片框。我可以用这种方式添加几个图像。
现在,我想允许用户操作和移动他们放入面板中的图像。
我试着寻找解决方案,但似乎找不到我能理解的答案。
非常感谢您的帮助。。
这是我当前的代码
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);
}
}
您可以在用户最初单击时获取鼠标位置,然后在PictureBox的MouseMove
事件中跟踪鼠标位置。您可以将这些处理程序附加到多个PictureBox。
private int xPos;
private int yPos;
private void pb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
}
}
private void pb_MouseMove(object sender, MouseEventArgs e)
{
PictureBox p = sender as PictureBox;
if(p != null)
{
if (e.Button == MouseButtons.Left)
{
p.Top += (e.Y - yPos);
p.Left += (e.X - xPos);
}
}
}
对于动态图片框,您可以附加类似的处理程序
PictureBox dpb = new PictureBox();
dpb.MouseDown += pb_MouseDown;
dbp.MouseMove += pb_MouseMove;
//fill the rest of the properties...