1.给接收图片的窗体和容器设置属性AllowDrop为True。
2.给窗体或者接收的容器添加DragDrop事件和DragEnter
DragDrop:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] rs = (string[])e.Data.GetData(DataFormats.FileDrop);
//MessageBox.Show(rs[0]);//rs[0]是图片的路径
pictureBox1.Load(rs[0]);//加载对应路径的图片
}
DragEnter:
//只允许文件拖放
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}