PictureEdit中拖放图片
public partial class Form2 : Form
{
string fileName = string.Empty;
public Form2()
{
InitializeComponent();
InitData();
}
private void InitData()
{
foreach (Control v in this.Controls)
{
if (v.GetType().Name == "PictureEdit")
{
v.AllowDrop = true;
v.DragEnter += new DragEventHandler(pictureEdit1_DragEnter);
v.DragDrop += new DragEventHandler(pictureEdit1_DragDrop);
}
}
}
void pictureEdit1_DragDrop(object sender, DragEventArgs e)
{
fileName=(e.Data.GetData(DataFormats.FileDrop,false)as string[])[0];
try
{
((PictureEdit)sender).Image=Image.FromFile(fileName);
}
catch (Exception)
{
MessageBox.Show("文件格式不对");
}
}
void pictureEdit1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
}