实现从外部拖放图片到窗体上的picturebox中;
1,新建一个窗体FORM2,窗体上放一个picturebox
把该窗体的ALLOWDROP属性设为TRUE;
2下面是实现这拖放的代码: (在FORM2的DragEnter,DragDrop事件中写如下代码就OK了)
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private string imageString = string.Empty;//图片路径
private void Form2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
}
private void Form2_DragDrop(object sender, DragEventArgs e)
{
string[] myFiles;
myFiles = (string[])(e.Data.GetData(DataFormats.FileDrop));
for (int i = 0; i < myFiles.Length; i++)
{
imageString = myFiles[i];
}
if (imageString != string.Empty)
{
pictureBox1.Image = Image.FromFile(imageString);
}
}
}